blob: 59d533df9480c541b8fa9d6ba1e57b4c14a371aa [file] [log] [blame]
Wenzel Jakob5cd33112015-10-20 00:58:59 +02001Benchmark
2=========
3
4The following is the result of a synthetic benchmark comparing both compilation
Wenzel Jakob192eb882016-08-19 09:38:14 +02005time and module size of pybind11 against Boost.Python. A detailed report about a
6Boost.Python to pybind11 conversion of a real project is available here: [#f1]_.
7
8.. [#f1] http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf
Wenzel Jakob5cd33112015-10-20 00:58:59 +02009
Wenzel Jakob66c9a402016-01-17 22:36:36 +010010Setup
11-----
12
13A python script (see the ``docs/benchmark.py`` file) was used to generate a set
14of files with dummy classes whose count increases for each successive benchmark
15(between 1 and 2048 classes in powers of two). Each class has four methods with
Wenzel Jakob5cd33112015-10-20 00:58:59 +020016a randomly generated signature with a return value and four arguments. (There
17was no particular reason for this setup other than the desire to generate many
18unique function signatures whose count could be controlled in a simple way.)
19
20Here is an example of the binding code for one class:
21
22.. code-block:: cpp
23
24 ...
25 class cl034 {
26 public:
27 cl279 *fn_000(cl084 *, cl057 *, cl065 *, cl042 *);
28 cl025 *fn_001(cl098 *, cl262 *, cl414 *, cl121 *);
29 cl085 *fn_002(cl445 *, cl297 *, cl145 *, cl421 *);
30 cl470 *fn_003(cl200 *, cl323 *, cl332 *, cl492 *);
31 };
32 ...
33
Dean Moldovan443ab592017-04-24 01:51:44 +020034 PYBIND11_MODULE(example, m) {
Wenzel Jakob5cd33112015-10-20 00:58:59 +020035 ...
36 py::class_<cl034>(m, "cl034")
37 .def("fn_000", &cl034::fn_000)
38 .def("fn_001", &cl034::fn_001)
39 .def("fn_002", &cl034::fn_002)
40 .def("fn_003", &cl034::fn_003)
41 ...
Wenzel Jakob5cd33112015-10-20 00:58:59 +020042 }
43
44The Boost.Python version looks almost identical except that a return value
45policy had to be specified as an argument to ``def()``. For both libraries,
46compilation was done with
47
48.. code-block:: bash
49
Wenzel Jakob66c9a402016-01-17 22:36:36 +010050 Apple LLVM version 7.0.2 (clang-700.1.81)
Wenzel Jakob5cd33112015-10-20 00:58:59 +020051
52and the following compilation flags
53
54.. code-block:: bash
55
Wenzel Jakob66c9a402016-01-17 22:36:36 +010056 g++ -Os -shared -rdynamic -undefined dynamic_lookup -fvisibility=hidden -std=c++14
57
58Compilation time
59----------------
Wenzel Jakob5cd33112015-10-20 00:58:59 +020060
61The following log-log plot shows how the compilation time grows for an
Wenzel Jakob66c9a402016-01-17 22:36:36 +010062increasing number of class and function declarations. pybind11 includes many
63fewer headers, which initially leads to shorter compilation times, but the
64performance is ultimately fairly similar (pybind11 is 19.8 seconds faster for
65the largest largest file with 2048 classes and a total of 8192 methods -- a
66modest **1.2x** speedup relative to Boost.Python, which required 116.35
67seconds).
Wenzel Jakob5cd33112015-10-20 00:58:59 +020068
Wenzel Jakobf64feaf2016-04-28 14:33:45 +020069.. only:: not latex
70
71 .. image:: pybind11_vs_boost_python1.svg
72
73.. only:: latex
74
75 .. image:: pybind11_vs_boost_python1.png
Wenzel Jakob5cd33112015-10-20 00:58:59 +020076
Wenzel Jakob66c9a402016-01-17 22:36:36 +010077Module size
78-----------
79
80Differences between the two libraries become much more pronounced when
81considering the file size of the generated Python plugin: for the largest file,
82the binary generated by Boost.Python required 16.8 MiB, which was **2.17
83times** / **9.1 megabytes** larger than the output generated by pybind11. For
84very small inputs, Boost.Python has an edge in the plot below -- however, note
85that it stores many definitions in an external library, whose size was not
86included here, hence the comparison is slightly shifted in Boost.Python's
87favor.
Wenzel Jakob5cd33112015-10-20 00:58:59 +020088
Wenzel Jakobf64feaf2016-04-28 14:33:45 +020089.. only:: not latex
90
91 .. image:: pybind11_vs_boost_python2.svg
92
93.. only:: latex
94
95 .. image:: pybind11_vs_boost_python2.png
96
Wenzel Jakob5cd33112015-10-20 00:58:59 +020097