Cython allows one to compile code written using a superset of the Python programming language into C code for improved performance. I recently tried it in an attempt to speed up my solution to a Project Euler problem, which was ran slowly, even by pypy. After some amount of work, the cython-based solution outperformed the pure-Python code, without me having to reimplement it all in C or C++ or similar. So you too should consider using it, if you need the extra performance.
The rest of this post contains some Cython tips for using it well that I discovered in my (so far relatively limited) experimentation with it.
I noticed that
cdef int i; for in xrange(0, mymax)does not get optimised while
cdef int i; for i in range(mymax);does.
cdef int arrdeclares a local array of 100*100 elements , and keeps copying it, which is likely not what you want. On the other hand,
cdef int  * ptrgenerates a pointer.
“cdef” cannot be used inside loops.
It is a good idea to declare the C types as late as possible - only right before the time-critical code when you absolutely need to use them. One can initialize them from dynamically-typed python data structures.
One can copy+paste the compiler's invocation command on the command line and edit it to apply better optimisation flags. There may be a way to customise the compilation command by editing the “setup.py” file, but I didn't bother to figure it out yet.
One can and should inspect the generated .c file to see the resultant C code to see if anything is done sub-optimally.
Copyright by Shlomi Fish, 2017.