blob: c06bfb3956defa6ddf2c78d0034ba4e6ed0141c5 [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
5time and module size of pybind11 against Boost.Python.
6
7A python script (see the ``docs/benchmark.py`` file) was used to generate a
8set 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
10a randomly generated signature with a return value and four arguments. (There
11was no particular reason for this setup other than the desire to generate many
12unique function signatures whose count could be controlled in a simple way.)
13
14Here 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
40The Boost.Python version looks almost identical except that a return value
41policy had to be specified as an argument to ``def()``. For both libraries,
42compilation was done with
43
44.. code-block:: bash
45
46 Apple LLVM version 7.0.0 (clang-700.0.72)
47
48and the following compilation flags
49
50.. code-block:: bash
51
52 g++ -Os -shared -rdynamic -undefined dynamic_lookup -fvisibility=hidden -std=c++11
53
54The following log-log plot shows how the compilation time grows for an
55increasing number of class and function declarations. pybind11 includes fewer
56headers, which initially leads to shorter compilation times, but the
57performance is ultimately very similar (pybind11 is 1 second faster for the
58largest file, which is less than 1% of the total compilation time).
59
60.. image:: pybind11_vs_boost_python1.svg
61
62Differences between the two libraries become more pronounced when considering
63the file size of the generated Python plugin. Note that the plot below does not
64include the size of the Boost.Python shared library, hence Boost actually has a
65slight advantage.
66
67.. image:: pybind11_vs_boost_python2.svg
68
69Despite this, the libraries procuced by Boost.Python for more than a few
70functions are consistently larger by a factor of 1.75.