[Tutor] learning to program in cython

Albert-Jan Roskam fomcl at yahoo.com
Thu Jan 17 12:52:41 CET 2013


>> With the help of an awesome python community I have been able to pick up the

>> language and now willing to explore other cool extensions of it.
>
>Good work!
>
>>
>> I routinely have large loops which could be ported to cython for speed.
>> However I have never written a single line of cython code. Any pointers on
>> getting started.
>
>There are two reasons for using cython:
>1) To interface directly with existing C libraries.
>2) To speed up CPU-intensive code.
>
>It sounds like you're interested in case 2). However, not all loops
>benefit from cythonisation. Loosely cython is good when
>a) you're doing something that can be written in a small amount of
>efficient C code
>b) but the corresponding Python code involves a lot of repeated
>function calls or expression evaluations.
>
>If you're already familiar with C then you'll probably have some idea
>when a) and b) apply. I would say that a prerequisite for learning to
>speed up CPU-intensive code with cython would be learning to use the
>python profilers. In particular you should learn to use cProfile and
>timeit:

(and of course pstats)

I recently used Cython for the first time and I found it surprisingly easy. The installation under Linux
is easy, but seems to be not really trivial under Windows (I never tried installing it under windows though; I'd first try one of the unofficial binaries: http://www.lfd.uci.edu/~gohlke/pythonlibs/#cython). One gotcha that I only found out later: it is possible to generate an html report of your Cythonized code. The yellower the code is coloured, the more it might still benefit from speed improvements: http://stackoverflow.com/questions/11058933/cython-a-flag-to-generate-yellow-shaded-html-without-command-line

It's pretty cool to use cProfile to make a Cython-Python comparison of the function you're trying to speed up. In my case it was 87 % faster IIRC (this difference was not only significant, but also relevent: a use case where this would save 15 minutes is totally realistic) . Yaay!

Albert-Jan


More information about the Tutor mailing list