benchmark.rst 3.1 KB

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