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.

96 lines
3.0 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.
  5. Setup
  6. -----
  7. A python script (see the ``docs/benchmark.py`` file) was used to generate a set
  8. of files with dummy classes whose count increases for each successive benchmark
  9. (between 1 and 2048 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. Here is an example of the binding code for one class:
  14. .. code-block:: cpp
  15. ...
  16. class cl034 {
  17. public:
  18. cl279 *fn_000(cl084 *, cl057 *, cl065 *, cl042 *);
  19. cl025 *fn_001(cl098 *, cl262 *, cl414 *, cl121 *);
  20. cl085 *fn_002(cl445 *, cl297 *, cl145 *, cl421 *);
  21. cl470 *fn_003(cl200 *, cl323 *, cl332 *, cl492 *);
  22. };
  23. ...
  24. PYBIND11_PLUGIN(example) {
  25. py::module m("example");
  26. ...
  27. py::class_<cl034>(m, "cl034")
  28. .def("fn_000", &cl034::fn_000)
  29. .def("fn_001", &cl034::fn_001)
  30. .def("fn_002", &cl034::fn_002)
  31. .def("fn_003", &cl034::fn_003)
  32. ...
  33. return m.ptr();
  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