Skip to content

Commit 5cd3311

Browse files
author
Wenzel Jakob
committed
added benchmark
1 parent 0fb8528 commit 5cd3311

File tree

5 files changed

+1027
-0
lines changed

5 files changed

+1027
-0
lines changed

docs/benchmark.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import random
2+
import os
3+
import time
4+
import datetime as dt
5+
6+
nfns = 4 # Functions per class
7+
nargs = 4 # Arguments per function
8+
9+
10+
def generate_dummy_code_pybind11(nclasses=10):
11+
decl = ""
12+
bindings = ""
13+
14+
for cl in range(nclasses):
15+
decl += "class cl%03i;\n" % cl
16+
decl += '\n'
17+
18+
for cl in range(nclasses):
19+
decl += "class cl%03i {\n" % cl
20+
decl += "public:\n"
21+
bindings += ' py::class_<cl%03i>(m, "cl%03i")\n' % (cl, cl)
22+
for fn in range(nfns):
23+
ret = random.randint(0, nclasses - 1)
24+
params = [random.randint(0, nclasses - 1) for i in range(nargs)]
25+
decl += " cl%03i *fn_%03i(" % (ret, fn)
26+
decl += ", ".join("cl%03i *" % p for p in params)
27+
decl += ");\n"
28+
bindings += ' .def("fn_%03i", &cl%03i::fn_%03i)\n' % \
29+
(fn, cl, fn)
30+
decl += "};\n\n"
31+
bindings += ' ;\n'
32+
33+
result = "#include <pybind11/pybind11.h>\n\n"
34+
result += "namespace py = pybind11;\n\n"
35+
result += decl + '\n'
36+
result += "PYBIND11_PLUGIN(example) {\n"
37+
result += " py::module m(\"example\");"
38+
result += bindings
39+
result += " return m.ptr();"
40+
result += "}"
41+
return result
42+
43+
44+
def generate_dummy_code_boost(nclasses=10):
45+
decl = ""
46+
bindings = ""
47+
48+
for cl in range(nclasses):
49+
decl += "class cl%03i;\n" % cl
50+
decl += '\n'
51+
52+
for cl in range(nclasses):
53+
decl += "class cl%03i {\n" % cl
54+
decl += "public:\n"
55+
bindings += ' py::class_<cl%03i>("cl%03i")\n' % (cl, cl)
56+
for fn in range(nfns):
57+
ret = random.randint(0, nclasses - 1)
58+
params = [random.randint(0, nclasses - 1) for i in range(nargs)]
59+
decl += " cl%03i *fn_%03i(" % (ret, fn)
60+
decl += ", ".join("cl%03i *" % p for p in params)
61+
decl += ");\n"
62+
bindings += ' .def("fn_%03i", &cl%03i::fn_%03i, py::return_value_policy<py::manage_new_object>())\n' % \
63+
(fn, cl, fn)
64+
decl += "};\n\n"
65+
bindings += ' ;\n'
66+
67+
result = "#include <boost/python.hpp>\n\n"
68+
result += "namespace py = boost::python;\n\n"
69+
result += decl + '\n'
70+
result += "BOOST_PYTHON_MODULE(example) {\n"
71+
result += bindings
72+
result += "}"
73+
return result
74+
75+
76+
for codegen in [generate_dummy_code_pybind11, generate_dummy_code_boost]:
77+
print ("{")
78+
for i in range(0, 10):
79+
nclasses = 2 ** i
80+
with open("test.cpp", "w") as f:
81+
f.write(codegen(nclasses))
82+
n1 = dt.datetime.now()
83+
os.system("g++ -Os -shared -rdynamic -undefined dynamic_lookup "
84+
"-fvisibility=hidden -std=c++11 test.cpp -I include "
85+
"-I /System/Library/Frameworks/Python.framework/Headers -o test.so")
86+
n2 = dt.datetime.now()
87+
elapsed = (n2 - n1).total_seconds()
88+
size = os.stat('test.so').st_size
89+
print(" {%i, %f, %i}," % (nclasses * nfns, elapsed, size))
90+
print ("}")

docs/benchmark.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Benchmark
2+
=========
3+
4+
The following is the result of a synthetic benchmark comparing both compilation
5+
time and module size of pybind11 against Boost.Python.
6+
7+
A python script (see the ``docs/benchmark.py`` file) was used to generate a
8+
set of dummy classes whose count increases for each successive benchmark
9+
(between 1 and 512 classes in powers of two). Each class has four methods with
10+
a randomly generated signature with a return value and four arguments. (There
11+
was no particular reason for this setup other than the desire to generate many
12+
unique function signatures whose count could be controlled in a simple way.)
13+
14+
Here is an example of the binding code for one class:
15+
16+
.. code-block:: cpp
17+
18+
...
19+
class cl034 {
20+
public:
21+
cl279 *fn_000(cl084 *, cl057 *, cl065 *, cl042 *);
22+
cl025 *fn_001(cl098 *, cl262 *, cl414 *, cl121 *);
23+
cl085 *fn_002(cl445 *, cl297 *, cl145 *, cl421 *);
24+
cl470 *fn_003(cl200 *, cl323 *, cl332 *, cl492 *);
25+
};
26+
...
27+
28+
PYBIND11_PLUGIN(example) {
29+
py::module m("example");
30+
...
31+
py::class_<cl034>(m, "cl034")
32+
.def("fn_000", &cl034::fn_000)
33+
.def("fn_001", &cl034::fn_001)
34+
.def("fn_002", &cl034::fn_002)
35+
.def("fn_003", &cl034::fn_003)
36+
...
37+
return m.ptr();
38+
}
39+
40+
The Boost.Python version looks almost identical except that a return value
41+
policy had to be specified as an argument to ``def()``. For both libraries,
42+
compilation was done with
43+
44+
.. code-block:: bash
45+
46+
Apple LLVM version 7.0.0 (clang-700.0.72)
47+
48+
and the following compilation flags
49+
50+
.. code-block:: bash
51+
52+
g++ -Os -shared -rdynamic -undefined dynamic_lookup -fvisibility=hidden -std=c++11
53+
54+
The following log-log plot shows how the compilation time grows for an
55+
increasing number of class and function declarations. pybind11 includes fewer
56+
headers, which initially leads to shorter compilation times, but the
57+
performance is ultimately very similar (pybind11 is 1 second faster for the
58+
largest file, which is less than 1% of the total compilation time).
59+
60+
.. image:: pybind11_vs_boost_python1.svg
61+
62+
Differences between the two libraries become more pronounced when considering
63+
the file size of the generated Python plugin. Note that the plot below does not
64+
include the size of the Boost.Python shared library, hence Boost actually has a
65+
slight advantage.
66+
67+
.. image:: pybind11_vs_boost_python2.svg
68+
69+
Despite this, the libraries procuced by Boost.Python for more than a few
70+
functions are consistently larger by a factor of 1.75.

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ Contents:
1212
classes
1313
advanced
1414
cmake
15+
benchmark
1516
reference

0 commit comments

Comments
 (0)