Pyrex, Cython, Numba, and Shedskin. These four projects let you translate Python code into C, C++ or LLVM code.
Shedskin compiles plain Python code into C++, while Pyrex and Cython (which is a fork of Pyrex) primarily target C.
In Cython (and Pyrex, its predecessor), you can add optional type declarations to your code, stating that
a variable is (and will always be) an integer, for example. In Cython, there is also interoperability support for NumPy
arrays, letting you write low-level code that accesses the array contents efficiently. I have used this in my own code,
achieving speedup factors of up to 300–400 for suitable code. The code that is generated by Pyrex and Cython can be
compiled directly to an extension module that can be imported into Python. If you want to generate C code from your
Python, Cython is a safe bet. If you’re just looking for the speedup, particularly for array-oriented and math-heavy
code, you should look into Numba, which generates LLVM code at import time. With the premium features available
in NumbaPro, there’s even GPU support.
Do'stlaringiz bilan baham: |