pythonjit
Automatically cross compiles .py files to .so (or .pyd on Windows) using Cython and import hooks.
How to use
Simply include the following at the earliest available point in your code:
import pythonjit
pythonjit.enable()
And any futher imports will automatically be cythonized, providing a modest performance benefit for no extra work and without compromising the pure python nature of source files.
For more performance benefits, use the decorators provided by cython to statically type variables and return types. These will not compromise the pure python nature of your source code - .py files that use these decorators can still be ran by the regular python interpreter.
The relevant decorators are:
@cython.cfunc
(declares cdef function)@cython.ccall
(declares cpdef function)@cython.returns
(declare return type)@cython.locals
(declare local variable types)@cython.cclass
(declare cdef class)@cython.inline
(equivalent to C inline keyword)@cython.final
(makes subclassing impossible, enabling optimizations)
Types are also part of the cython module:
cython.int
cython.longlong
cython.struct
cython.union
cython.typedef
cython.cast
- etc
For more information on cython types and decorators, see the cython docs
Bugs in your code and compilation time
Cython is more stringent than the python interpreter when it comes to catching errors in code. For example, it will complain about NameErrors that occur in unreachable code, which the python interpreter will not do. A larger/complex project may have issues to fix before Cython can compile it.
Cross compilation can take some time, especially for larger projects. A caching mechanism is used to compensate for this, so the first run will be slower than future runs.
Requirements:
cython
must be installedgcc
must be available (Windows users should install MinGW to gain access togcc
)
Notes/Known issues/Complications:
__file__
may not be available in compiled modules - see here for a workaround- Supports python 2 and 3, but the default version is set to 2.