You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.1 KiB

  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_PLUGIN(example) {
  27. py::module m("example");
  28. ...
  29. py::class_<cl034>(m, "cl034")
  30. .def("fn_000", &cl034::fn_000)
  31. .def("fn_001", &cl034::fn_001)
  32. .def("fn_002", &cl034::fn_002)
  33. .def("fn_003", &cl034::fn_003)
  34. ...
  35. return m.ptr();
  36. }
  37. The Boost.Python version looks almost identical except that a return value
  38. policy had to be specified as an argument to ``def()``. For both libraries,
  39. compilation was done with
  40. .. code-block:: bash
  41. Apple LLVM version 7.0.2 (clang-700.1.81)
  42. and the following compilation flags
  43. .. code-block:: bash
  44. g++ -Os -shared -rdynamic -undefined dynamic_lookup -fvisibility=hidden -std=c++14
  45. Compilation time
  46. ----------------
  47. The following log-log plot shows how the compilation time grows for an
  48. increasing number of class and function declarations. pybind11 includes many
  49. fewer headers, which initially leads to shorter compilation times, but the
  50. performance is ultimately fairly similar (pybind11 is 19.8 seconds faster for
  51. the largest largest file with 2048 classes and a total of 8192 methods -- a
  52. modest **1.2x** speedup relative to Boost.Python, which required 116.35
  53. seconds).
  54. .. only:: not latex
  55. .. image:: pybind11_vs_boost_python1.svg
  56. .. only:: latex
  57. .. image:: pybind11_vs_boost_python1.png
  58. Module size
  59. -----------
  60. Differences between the two libraries become much more pronounced when
  61. considering the file size of the generated Python plugin: for the largest file,
  62. the binary generated by Boost.Python required 16.8 MiB, which was **2.17
  63. times** / **9.1 megabytes** larger than the output generated by pybind11. For
  64. very small inputs, Boost.Python has an edge in the plot below -- however, note
  65. that it stores many definitions in an external library, whose size was not
  66. included here, hence the comparison is slightly shifted in Boost.Python's
  67. favor.
  68. .. only:: not latex
  69. .. image:: pybind11_vs_boost_python2.svg
  70. .. only:: latex
  71. .. image:: pybind11_vs_boost_python2.png