Shlomi Fish (shlomif) wrote in shlomif_tech,
Shlomi Fish

  • Music:

Cython: Report and Tips

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.

  1. 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.

  2. cdef int arr[100][100] declares a local array of 100*100 elements , and keeps copying it, which is likely not what you want. On the other hand, cdef int [100] * ptr generates a pointer.

  3. “cdef” cannot be used inside loops.

  4. 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.

  5. 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 “” file, but I didn't bother to figure it out yet.

  6. 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.

You can reuse this entry under the Creative Commons Attribution 3.0 Unported licence, or at your option any later version of it. See the instructions on how to comply with it.

Tags: foss, linux, open-source, tech, tech tip, tip
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened