Benchmarks
This commit is contained in:
1 parent
ed4db5e39f
commit
e67009862f
8 files changed
+263
No files matched your search
@@ -0,0 +1,57 @@
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
||||
'..', 'build', 'default', 'benchmarks'))
|
||||
|
||||
from timeit import Timer
|
||||
|
||||
TIMES = 10000000
|
||||
|
||||
|
||||
|
||||
def bench(mod):
|
||||
def bench3():
|
||||
return mod.Multiplier()
|
||||
print "test3 (call class constructor with no arguments):", Timer(bench3).timeit(TIMES)
|
||||
|
||||
def bench4():
|
||||
return mod.Multiplier(3.0)
|
||||
print "test4 (call class constructor with double):", Timer(bench4).timeit(TIMES)
|
||||
|
||||
obj = mod.Multiplier(3.0)
|
||||
def bench5():
|
||||
return obj.GetFactor()
|
||||
print "test4 (call method with no arguments):", Timer(bench5).timeit(TIMES)
|
||||
|
||||
obj = mod.Multiplier(3.0)
|
||||
def bench6():
|
||||
return obj.Multiply(5.0)
|
||||
print "test4 (call method with double):", Timer(bench6).timeit(TIMES)
|
||||
|
||||
def bench1():
|
||||
return mod.func1()
|
||||
print "test1 (call function with no arguments):", Timer(bench1).timeit(TIMES)
|
||||
|
||||
def bench2():
|
||||
return mod.func2(1.0, 2.0, 3.0)
|
||||
print "test2 (call function taking 3 doubles):", Timer(bench2).timeit(TIMES)
|
||||
|
||||
|
||||
def main():
|
||||
import testapi_pybindgen
|
||||
import testapi_boost
|
||||
import testapi_swig
|
||||
|
||||
print "pybindgen results:"
|
||||
bench(testapi_pybindgen)
|
||||
|
||||
print "boost_python results:"
|
||||
bench(testapi_boost)
|
||||
|
||||
print "swig results:"
|
||||
bench(testapi_swig)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,30 @@
|
||||
#! /usr/bin/env python
|
||||
|
||||
import sys
|
||||
|
||||
from pybindgen import Module, retval, param, FileCodeSink
|
||||
|
||||
|
||||
def my_module_gen(out_file):
|
||||
|
||||
mod = Module('testapi_pybindgen')
|
||||
mod.add_include('"testapi.h"')
|
||||
|
||||
mod.add_function('func1', None, [])
|
||||
mod.add_function('func2', 'double', [param('double', 'x'),
|
||||
param('double', 'y'),
|
||||
param('double', 'z'),
|
||||
])
|
||||
|
||||
Multiplier = mod.add_class('Multiplier', allow_subclassing=True)
|
||||
Multiplier.add_constructor([])
|
||||
Multiplier.add_constructor([param('double', 'factor')])
|
||||
|
||||
Multiplier.add_method('GetFactor', 'double', [], is_const=True)
|
||||
Multiplier.add_method('Multiply', 'double', [param('double', 'value')], is_virtual=True, is_const=True)
|
||||
|
||||
|
||||
mod.generate(FileCodeSink(out_file))
|
||||
|
||||
if __name__ == '__main__':
|
||||
my_module_gen(sys.stdout)
|
||||
@@ -0,0 +1,36 @@
|
||||
// -*- Mode: C++; c-file-style: "stroustrup"; indent-tabs-mode:nil; -*-
|
||||
#include "testapi.h"
|
||||
|
||||
void func1 (void)
|
||||
{
|
||||
}
|
||||
|
||||
double func2 (double x, double y, double z)
|
||||
{
|
||||
return x + y + z;
|
||||
}
|
||||
|
||||
|
||||
Multiplier::Multiplier ()
|
||||
: m_factor (1.0)
|
||||
{
|
||||
}
|
||||
|
||||
Multiplier::Multiplier (double factor)
|
||||
: m_factor (factor)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
double Multiplier::GetFactor () const
|
||||
{
|
||||
return m_factor;
|
||||
}
|
||||
|
||||
double
|
||||
Multiplier::Multiply (double value) const
|
||||
{
|
||||
return value*m_factor;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// -*- Mode: C++; c-file-style: "stroustrup"; indent-tabs-mode:nil; -*-
|
||||
#ifndef TESTAPI_H_
|
||||
# define TESTAPI_H_
|
||||
|
||||
|
||||
void func1 (void);
|
||||
|
||||
double func2 (double x, double y, double z);
|
||||
|
||||
class Multiplier
|
||||
{
|
||||
double m_factor;
|
||||
|
||||
public:
|
||||
Multiplier ();
|
||||
Multiplier (double factor);
|
||||
|
||||
double GetFactor () const;
|
||||
virtual double Multiply (double value) const;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
// -*- Mode: C++; c-file-style: "stroustrup"; indent-tabs-mode:nil; -*-
|
||||
#include "testapi.h"
|
||||
|
||||
#include <boost/python.hpp>
|
||||
|
||||
BOOST_PYTHON_MODULE(testapi_boost)
|
||||
{
|
||||
using namespace boost::python;
|
||||
def("func1", func1);
|
||||
def("func2", func2);
|
||||
|
||||
class_<Multiplier>("Multiplier")
|
||||
.def(init<double>())
|
||||
.def("GetFactor", &Multiplier::GetFactor)
|
||||
.def("Multiply", &Multiplier::Multiply)
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/* example.i */
|
||||
|
||||
%module testapi_swig
|
||||
%{
|
||||
|
||||
#include "testapi.h"
|
||||
|
||||
%}
|
||||
|
||||
extern void func1(void);
|
||||
extern double func2(double x, double y, double z);
|
||||
|
||||
class Multiplier
|
||||
{
|
||||
double m_factor;
|
||||
|
||||
public:
|
||||
Multiplier ();
|
||||
Multiplier (double factor);
|
||||
|
||||
double GetFactor () const;
|
||||
virtual double Multiply (double value) const;
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
## -*- python -*-
|
||||
|
||||
def configure(conf):
|
||||
pass
|
||||
|
||||
def build(bld):
|
||||
|
||||
#
|
||||
# PyBindGen
|
||||
#
|
||||
gen = bld.new_task_gen(
|
||||
features='command',
|
||||
source='testapi-pybindgen.py',
|
||||
target='testapimodule.cc',
|
||||
command='${PYTHON} ${SRC[0]} > ${TGT[0]}')
|
||||
|
||||
obj = bld.new_task_gen('cxx', 'shlib', 'pyext')
|
||||
obj.source = [
|
||||
'testapi.cc',
|
||||
'testapimodule.cc'
|
||||
]
|
||||
obj.target = 'testapi_pybindgen'
|
||||
obj.install_path = None # do not install
|
||||
obj.includes = '.'
|
||||
|
||||
|
||||
#
|
||||
# Boost::Python
|
||||
#
|
||||
obj = bld.new_task_gen('cxx', 'shlib', 'pyext')
|
||||
obj.source = [
|
||||
'testapi.cc',
|
||||
'testapi_boost.cc'
|
||||
]
|
||||
obj.target = 'testapi_boost'
|
||||
obj.install_path = None # do not install
|
||||
obj.includes = '.'
|
||||
obj.env.append_value('LIB', 'boost_python-mt')
|
||||
|
||||
|
||||
#
|
||||
# SWIG
|
||||
#
|
||||
|
||||
gen = bld.new_task_gen(
|
||||
features='command',
|
||||
source='testapi_swig.i',
|
||||
target='testapi_swig_module.cc',
|
||||
command='swig -c++ -o ${TGT[0]} -python ${SRC[0]}')
|
||||
|
||||
obj = bld.new_task_gen('cxx', 'shlib', 'pyext')
|
||||
obj.source = [
|
||||
'testapi.cc',
|
||||
'testapi_swig_module.cc'
|
||||
]
|
||||
obj.target = '_testapi_swig'
|
||||
obj.install_path = None # do not install
|
||||
obj.includes = '.'
|
||||
|
||||
@@ -283,6 +283,8 @@ def configure(conf):
|
||||
conf.env.append_value('CXXFLAGS_PYEXT', '-fvisibility=hidden')
|
||||
conf.env.append_value('CCFLAGS_PYEXT', '-fvisibility=hidden')
|
||||
|
||||
conf.sub_config('benchmarks')
|
||||
|
||||
|
||||
def build(bld):
|
||||
global g_bld
|
||||
@@ -296,6 +298,20 @@ def build(bld):
|
||||
if Options.commands['check'] or Options.commands['clean']:
|
||||
bld.add_subdirs('tests')
|
||||
|
||||
if Options.commands.get('bench', False) or Options.commands['clean']:
|
||||
bld.add_subdirs('benchmarks')
|
||||
|
||||
|
||||
check_context = Build.BuildContext
|
||||
def bench(bld):
|
||||
"run the benchmarks; requires many tools, used by maintainers only"
|
||||
Scripting.build(bld)
|
||||
env = g_bld.env
|
||||
|
||||
print "Running benchmarks..."
|
||||
retval = subprocess.Popen([env['PYTHON'], '-O', 'benchmarks/bench.py']).wait()
|
||||
|
||||
|
||||
check_context = Build.BuildContext
|
||||
def check(bld):
|
||||
"run the unit tests; add option -v for verbose mode"
|
||||
|
||||
Reference in new issue
Block a user