Pythran as a Numpy backend¶
Using the flag--np-pythran
, it is possible to use thePythran numpyimplementation for numpy related operations. One advantage to use this backendis that the Pythran implementation uses C++ expression templates to save memorytransfers and can benefit from SIMD instructions of modern CPU.
This can lead to really interesting speedup in some cases, going from 2 up to16, depending on the targeted CPU architecture and the original algorithm.
Please note that this feature is experimental.
Usage example with setuptools¶
You first need to install Pythran. See itsdocumentation for more information.
Then, simply add acython:np_pythran=True
directive at the top of thePython files that needs to be compiled using Pythran numpy support.
Here is an example of a simplesetup.py
file using setuptools:
fromsetuptoolsimportsetupfromCython.Buildimportcythonizeimportnumpyimportpythransetup(name="My hello app",ext_modules=cythonize('hello_pythran.pyx'),include_dirs=[numpy.get_include(),pythran.get_include()])
Then, with the following header inhello_pythran.pyx
:
# cython: np_pythran=True
hello_pythran.pyx
will be compiled using Pythran numpy support.
Please note that Pythran can further be tweaked by adding settings in the$HOME/.pythranrc
file. For instance, this can be used to enableBoost.SIMD support.See thePythran user manual formore information.