Cython is 20 - Why Is It Ignored?
Written by Mike James   
Wednesday, 06 April 2022

OK, maybe it isn't ignored by those who are happy to make use of it, but it certainly isn't mainstream. Why should you care? The answer is speed and interoperability with C/C++.

Cythpnbanner

When you say Python this generally is taken to mean Python as defined by the "standard" Python implementation, CPython. But there are other implementations. Of course, given how lightly Python is defined as a standard, it can be argued that these aren't other implementations but interpretations of the same general language idea. This is a little unfair because, unless you make use of some of the darker corners of Python or the very latest additions, most of the implementations do a good job of running Python 3 programs.

A recent blog post by Stefan Behnel recounts some of the early days of Cython and ideas behind it. The basic motivation was that writing C extensions to CPython is difficult. As one who has suffered both Ctypes and extensions, I can tell you that Cython is a much easier way of doing the job. It will seem even easier if you don't program in C.  To quote from the documentation:

Originally based on the well-known Pyrex, the Cython project has approached this problem by means of a source code compiler that translates Python code to equivalent C code. This code is executed within the CPython runtime environment, but at the speed of compiled C and with the ability to call directly into C libraries. At the same time, it keeps the original interface of the Python source code, which makes it directly usable from Python code. These two-fold characteristics enable Cython’s two major use cases: extending the CPython interpreter with fast binary modules, and interfacing Python code with external C libraries.

Notice that Cython compiles Python to C and as a result runs anything up to 20 times faster than CPython. Notice that the Cython code also runs under the CPython interpreter, which is itself written in C. To get the best speed increases you also need to take advantage of the static C-like data types, which make Cython a superset of Python rather than just a clone.

Cython is an optimising static compiler for both the Python programming language and the extended Cython programming language (based on Pyrex). It makes writing C extensions for Python as easy as Python itself.

If Cython is so good why aren't we all using it?

One good reason is that it is a pure compiler and this is just not as easy to use during development. Another good reason is that it just isn't core Python - CPython is the real thing. You can simply put Cython into the same basket as all the other alternatives - Jython, a Java JVM implemention; IronPython, a C# .NET compiler; and PyPy which is implemented in Python itself! Then there is MicroPython, which targets IoT type devices. All-in-all it's a big bag of mixed alternatives, some of which have had close shaves with existence - IronPython for example.

A good question is why the Python mainstream don't take notice of some of the really good ideas in Cython? Its static types would be easy enough to add to CPython and would give a good speed improvment. I think it's a case of "not invented here".

Even so, Happy 20th Birthday Cython. If you haven't tried it then it is worth a look as you can use it without abandoning CPython.

Cythpnicon

 

More Information

Cython is 20!

https://cython.org/

Related Articles

Guido And Microsoft Want To Make Python x2 Faster

Pyston 2.2 Goes Open Source Again

Too Much Py In PyPI

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


Avi Wigderson Gains Turing Award
16/04/2024

Israeli mathematician and computer scientist, Avi Wigderson, is the recipient of the 2023 ACM A.M Turing Award which carries a $1 million prize with financial support from Google.



The University of Tübingen's Self-Driving Cars Course
22/03/2024

The recorded lectures and the written material of a course on Self-Driving Cars at the University of Tübingen have been made available for free. It's a first class opportunity to learn the in an [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Wednesday, 06 April 2022 )