The CPython + Cython implementation is the fastest; it is 44 times faster than the CPython implementation. This is an impressive speed improvement, especially considering that the Cython code is very close to the original Python code in its design.
Note that regular Python takes more than 500 seconds for executing the above code while Cython just takes around 1 second. Thus, Cython is 500x times faster than Python for summing 1 billion numbers.
Because of its JIT compiler, the PyPy is faster than CPython. Its last stable release was 7.1 on 24 March 2019. PyPy and Cython, both are chosen when speed is critical or a requirement in the matter. PyPy, an implementation in Python itself makes its programs run faster with the just-in-time compiler.
Even Cython will be several times slower than a carefully tuned C/C++, Java, C# or Go program for most practical problems. And at least in the case of C/C++ it'll also likely use several times more RAM.
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:
That’s a 100× speed-up. Not too shabby.
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.
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.
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.
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).
Biggest reason Cython is not that popular is that it lacks standalone (without python) executables.
Lack of publicity. The developers seem to be academics more interested in Developing their Sage software, than a cutting edge language.
Pitfalls encountered during development. One I encountered is lack of true thread support. Everything is wrapped up in a global interpreter lock, making it threadsafe, but disabling concurrency!
For anyone who wishes that cython actually compiled and ran your program in one line, I created runcython (http://github.com/russell91/runcython). runcython test.pyx
will have the semantics that the OP intended
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With