Cython Speed Boost vs. Usability

user277465 picture user277465 · Apr 23, 2010 · Viewed 28.2k times · Source

I just came across Cython, while I was looking out for ways to optimize Python code. I read various posts on Stack Overflow, the python wiki and read the article "General Rules for Optimization".

Cython is something which grasps my interest the most; instead of writing C-code for yourself, you can choose to have other data types in your python code itself.

Here is a silly test I tried,

#!/usr/bin/python
# test.pyx
def test(value):
    for i in xrange(value):
    i**2
    if(i==1000000):
        print i

test(10000001)

$ time python test.pyx

real    0m16.774s 
user    0m16.745s
sys     0m0.024s

$ time cython test.pyx

real    0m0.513s 
user    0m0.196s 
sys     0m0.052s

Now, honestly, I'm dumbfounded. The code which I have used here is pure python code, and all I have changed is the interpreter. In this case, if the cython is this good, then why do people still use the traditional Python interpreter? Are there any reliability issues for Cython?

Answer

Justin Peel picture Justin Peel · Apr 23, 2010

The other answers have already explained how you were just compiling the Cython code, not executing it. However, I thought that you might want to know how much faster Cython can make your code. When I compiled the code you have (though I ran the function from from a different module) with distutils, I got very marginal speed gains over straight Python – about 1%. However, when I added a few small changes to your code:

def test(long long value):
    cdef long long i
    cdef long long z
    for i in xrange(value):
        z = i**2
        if(i==1000000):
            print i
        if z < i:
            print "yes"

and compiled it, I got the following times:

  • Pure Python code: 20.4553578737 seconds
  • Cython code: 0.199339860234 seconds

That’s a 100× speed-up. Not too shabby.