Cython Speed Boost vs. Usability

Cython is not another interpreter. It generates c-extensions for python, from python(-like) code. cython test.pyx will only generate a 'test.c' file, which (once compiled) can be used by python just like a normal python library.

That means that you are only measuring the time it takes for cython to translate your python code to c, not how fast that version of your code runs.


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.


A big point that seems to be missing: Cython is not a strict superset of Python. There are some features that Python supports, but Cython does not. Most notably, generators and lambdas (but they are coming).


  • cython test.pyx doesn't actually run your program. The cython binary is for processing your Cython code into a Python extension module. You would have to import it in Python to run it.

  • #!/usr/bin/python isn't the best shebang line for Python scripts. #!/usr/bin/env python is generally preferred, which runs whatever python would on the command line.

    • Cython pyx files probably shouldn't have a shebang line at all, except in the corner case they are valid Python programs.
  • You have an IndentationError in the posted code.

  • Using the traditional interpreter is simpler and more portable. Cython is reliable, but has its limitations and quirks. It might be compelling to use it tons more if it magically gave the speedups your timings make it look like it does, but it actually gives smaller ones. You'll have to start using Cython-specific features to use C features to see a lot of speedup.