vendredi 12 décembre 2008

boost::python vs swig::python - a benchmark

Disclaimer : benchmarks are evil, this one is no different. It probably measures something that is completely irrelevant. But, you know, benchmarks are fun.

What I did : I generated a very simple C++ library : it contains only one-liners that return integer constants. Then I used boost::python and swig::python to make python wrappers for it.

The runtime test

The test is simple enough : I call one of the trivial functions 1000000 times and measure the time using this :


stmt = """
import fun
s = 0
for i in xrange(1000000):
s += fun.fun0()
"""
import timeit
T = timeit.Timer(stmt)
print min( T.repeat(10,1) )





There you have it : swig generates faster code in this useless, totally artificial, case.

When size matters

In many cases (embedded applications, for example) size matters. In the following graphic you can see the size of the linked pyd file (in kb) with 10, 100, 1000 and 10000 nearly identical on-liners generated.



Swig sure has a bigger size overhead when your are binding only a few functions. Then boost grows faster, maybe the infamous code bloat associated with template metaprogramming - but take this with a *huge* grain of salt.

The versions

I used boost 1.37 and swig 1.3.36 on windows vista with the microsoft toolchain (32 bits C/C++ compiler version 15.00.30729.01).
The C++ compiler flags were

/FC /O2 /arch:SSE /arch:SSE2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /D_SCL_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_DEPRECATE /FD /EHsc /GR /W3 /nologo /c /TP /errorReport:prompt


and the linker flags were

/NOLOGO /SUBSYSTEM:CONSOLE /MACHINE:X86 /OPT:REF /OPT:ICF


I'm still using python 2.5.2.

Aucun commentaire: