Skip to content

why pybind11 is slower than cython #1227

@zhuoqiang

Description

@zhuoqiang

simple benchmark for the example add() function

In [1]: import example
In [2]: %timeit example.add(1000, 2000)
606 ns ± 33.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

release buidl (using cmake -DCMAKE_BUILD_TYPE=Release .. ) improves the speed:

In [1]: import example
In [2]: %timeit example.add(1000, 2000)
469 ns ± 16.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

however, comparing with cython it still not fast enough:

In [10]: %reload_ext cython
In [11]:
    ...: %%cython
    ...: cpdef int cython_add(int a, int b):
    ...:     return a + b
    ...:
In [12]: %timeit cython_add(1000, 2000)
114 ns ± 5.48 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

pybind11 is 4X times slower than cython.

Why that happened? Is there anything to do to improve the performance here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions