diff --git a/benchmarks/bench.py b/benchmarks/bench.py new file mode 100644 index 0000000..2ebe18a --- /dev/null +++ b/benchmarks/bench.py @@ -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() diff --git a/benchmarks/testapi-pybindgen.py b/benchmarks/testapi-pybindgen.py new file mode 100644 index 0000000..faa434b --- /dev/null +++ b/benchmarks/testapi-pybindgen.py @@ -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) diff --git a/benchmarks/testapi.cc b/benchmarks/testapi.cc new file mode 100644 index 0000000..4909cdc --- /dev/null +++ b/benchmarks/testapi.cc @@ -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; +} + + diff --git a/benchmarks/testapi.h b/benchmarks/testapi.h new file mode 100644 index 0000000..94722b0 --- /dev/null +++ b/benchmarks/testapi.h @@ -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 diff --git a/benchmarks/testapi_boost.cc b/benchmarks/testapi_boost.cc new file mode 100644 index 0000000..4793032 --- /dev/null +++ b/benchmarks/testapi_boost.cc @@ -0,0 +1,19 @@ +// -*- Mode: C++; c-file-style: "stroustrup"; indent-tabs-mode:nil; -*- +#include "testapi.h" + +#include + +BOOST_PYTHON_MODULE(testapi_boost) +{ + using namespace boost::python; + def("func1", func1); + def("func2", func2); + + class_("Multiplier") + .def(init()) + .def("GetFactor", &Multiplier::GetFactor) + .def("Multiply", &Multiplier::Multiply) + ; + +} + diff --git a/benchmarks/testapi_swig.i b/benchmarks/testapi_swig.i new file mode 100644 index 0000000..81c0b87 --- /dev/null +++ b/benchmarks/testapi_swig.i @@ -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; +}; diff --git a/benchmarks/wscript b/benchmarks/wscript new file mode 100644 index 0000000..14153d6 --- /dev/null +++ b/benchmarks/wscript @@ -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 = '.' + diff --git a/wscript b/wscript index 8d5b8f5..ff0ecec 100644 --- a/wscript +++ b/wscript @@ -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"