Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost.python vs Cython for C++/python interface

I know this has been asked a thousand times, however i need to choose a library that can expose C++ functions and methods to python.

Considering my application, which mainly is a scientific (matrix) library, and python generator's matureness, support availability and performance, i have come across two options:

  • Cython
  • Boost.Python

I have eliminated (for good or bad reasons) others options like SWIG, SIP, Pybindgen, ...

Any advice regarding those 2 library ? Any of these which would have a killer feature ?

My project has namespaces, nested classes, callbacks, and the like.

Thanks

like image 442
Carmellose Avatar asked Oct 14 '13 08:10

Carmellose


People also ask

Can Cython be as fast as C?

Cython is the same speed as a carefully tuned C/C++ program; carefully tuned, Cython maps directly to C/C++. I've done many benchmarks of low level numerical code when implementing SageMath (which uses Cython for several 100K lines of code).

Is Cython better than Python?

Performance and Speed Despite being a superset of Python, Cython is much faster than Python. It improves Python code execution speed significantly by compiling Python code into C code. The compilation further helps developers to run the Python programs smoothly without deploying additional computing resources.

How do I make my Python as fast as C?

Although C remains the master of speed in general, PyPy can beat C in some cases. “If you want your code to magically run faster, you should probably just use PyPy.” PyPy is less effective when our program is fast anyway or when most of the runtime is spent for calls to non-python libraries.

Is CPython as fast as C?

Note that the optimization is not always 1000 times faster for all C code, but it should be at least 10 times faster, so it is still hundreds of times faster than Python. Also bear in mind that the speed difference between C and Python varies on different code.


1 Answers

Considering my application, which mainly is a scientific

That pretty much means Cython. Cython was made almost specifically for that very purpose. There is a video on getting started with on from Enthought. Enthought is a company that specializes in scientific calculations.

Cython has a very good integration with Python that is unmatched by C++ or C (you would have to use #include <Python.h>).

like image 50
Games Brainiac Avatar answered Sep 22 '22 12:09

Games Brainiac