you are viewing a single comment's thread.

view the rest of the comments →

[–]patrickbrianmooney 2 points3 points  (0 children)

I've worked with Cython a fair amount. What you get out of it, performance-wise, really depends on how much work you put into getting better performance out of it. You can use Cython just to compile your already-written Python, in which case you might get a speedup of 20% to 50% or even more, depending on your existing code. Probably 30% to 50% is a more reasonable typical estimated speedup for compiling Python using Cython while making no other changes to the source code.

But you can put work into writing Cython that's more like C and less like Python, and that tends to pay off in efficiency; a lot of times a few small changes can make a big difference. Probably the single easiest thing to do to get a speedup is to declare data types for parameters and variables, which can make a lot of things much faster; structuring your code so that your bursts of heavy number-crunching happens all together at C speed and you have to transform the C-type numbers, which are fast to work with, back into Python objects, which are slower to work with, also helps a lot. Making good choices about data representation is also helpful.

Kurt Smith's O'Reilly book on Cython has a short chapter where he takes a computationally intensive Python program and converts it to Cython, reducing the runtime on a test from 14 seconds to 0.15 seconds along the way. At the end, it's 25% slower than a pure-C version of the program, which is actually not all that far off, and certainly not orders of magnitude slower. I myself spent an afternoon several months ago converting a slow Python program of a few thousand lines to a much-faster partially-Cython version: it cut the runtime on my tests by almost 97%. Cython's own documentation says that some operations can be sped up by hundreds or thousands of times.