Cython is an optimising static compiler for both the
language and the extended Cython programming language (based on Pyrex).
It makes writing C extensions for Python as easy as Python itself.
Cython gives you the combined power of Python and C to let you
The Cython language is a superset of the Python language that
additionally supports calling C functions and declaring
C types on variables and class attributes. This allows the
compiler to generate very efficient C code from Cython code.
The C code is generated once and then compiles with all major
C/C++ compilers in CPython 2.6, 2.7 (2.4+
with Cython 0.20.x) as well as 3.2 and all later versions.
We regularly run integration tests against all supported CPython versions and
their latest in-development branches to make sure that the generated code stays
widely compatible and well adapted to each version.
PyPy support is work in progress (on both sides)
and is considered mostly usable
since Cython 0.17. The latest PyPy version is always recommended here.
All of this makes Cython the ideal language for wrapping
external C libraries, embedding CPython into existing
applications, and for fast C modules that speed up the
execution of Python code.
If you still have questions, feel free to send an email to the
cython users mailing list.
Aspects of the core development are discussed on the
cython core developer
mailing list. If you are unsure which list to use, the cython users list is probably the right one to use, which has the larger audience.
Cython is freely available under the open source Apache License.
The latest release of Cython is 0.22.1 (released 2015-06-20).
You can download it as a gzipped tar
or as a zip file.
Cython is also available from the PyPI package index repository.
Christoph Gohlke has created Windows installers available for download on
Dag Sverre Seljebotn.
Arfrever Frehtes Taifersar Arahesis,
Andreas van Cranenburgh,
Magnus Lie Hetland,
W. Trevor King,
Arfrever Taifersar Arahesis,
Google and Enthought funded Dag Seljebotn to greatly improve
Cython integration with NumPy.
Kurt Smith and Danilo Freitas were funded through the Google Summer of Code program to work on improved Fortran and C++ support respectively,
and in 2010 Haoyu Bai was funded to work on Python 3 compatibility.
to Greg Ewing for inventing and developing Cython's predecessor
and for his valuable input in language design decisions.
What users have to say about Cython:
»You would expect a whole lot of organizations and people to fancy a
language that's about as high-level as Python, yet almost as fast and
down-to-the-metal as C.
Add to that the ability to seamlessly integrate with both
your existing C/++ codebase and your Python codebase, easily mix very
high level abstractions with very low-level machine access... clear
Dun Peal on c.l.py
»You guys rock!
In scikit-learn, we have decided early on to do Cython, rather than C or
C++. That decision has been a clear win because the code is way more
maintainable. We have had to convince new contributors that Cython was
better for them, but the readability of the code, and the capacity to
support multiple Python versions, was worth it.« →
»The biggest surprise (and of course this is Cython's selling
point) is how simple the interfacing between high level and low level
code becomes, and the fact that it is all very robust.
It's exiciting to see that there are several active projects around
that attempt to speed up Python. The nice thing about Cython is that
it doesn't give you "half the speed of C" or "maybe nearly the speed
of C, 3 years from now" -- it gives the real deal, -O3 C, and it works
right now.« →
»I'm honestly never going back to writing C again. Cython gives
me all the expressiveness of Python combined with all the performance
and close-to-the-metal-godlike-powers of C. I've been using it to
implement high-performance graph traversal and routing algorithms and
to interface with C/C++ libraries, and it's been an absolute amazing
productivity boost.« →
»A general rule of thumb is that your program spends 80% of
its time running 20% of the code. Thus a good strategy for efficient
coding is to write everything, profile your code, and optimize the
parts that need it. Python’s profilers are great, and Cython allows
you to do the latter step with minimal effort.« →
»The question was, in auto-generated code, to what extent there
were bugs there, to what extent there were bugs in the generators. The
first time I did this, I got lots and lots of warnings from the tool for
code generated by both SWIG and Cython [...]
Basically, everything I found Cython emitting was a false positive and
a bug in my checker tool [CPyChecker].« →
»Basically, Cython is about 7x times faster than Boost.Python, which
astonished me.« →
»Using Cython allows you to just put effort into speeding up the
parts of code you need to work on, and to do so without having to
change very much. This is vastly different from ditching all the code
and reimplementing it another language. It also requires you to learn
a pretty minimal amount of stuff. You also get to keep the niceness of
the Python syntax which may Python coders have come to
»If you have a piece of Python that you need to run fast, then I
would recommend you used Cython immediately. This means that I can
exploit the beauty of Python and the speed of C together, and that’s a
match made in heaven.« →
»From 85 seconds (at the beginning of this post) down to 0.8
seconds: a reduction by a factor of 100 ...thank you cython!
»Writing a full-on CPython module from scratch would probably
offer better performance than Cython if you know the quirks and are
disciplined. But to someone who doesn't already drip CPython C
modules, Cython is a godsend.
Ultimately, there's 5 commonly used ways (CPython [C-API],
Boost::Python, SWIG, Cython, ctypes) to integrate C into Python, and
right now you'd be crazy not to give Cython a shot, if that's your
need. It's very easy to learn for anyone familiar with both C and
»What I loved about the Cython code is that I use a Python
list to manage the Vortex objects. This shows that we can use the
normal Python containers to manage objects. This is extremely
Clearly, if you are building code from scratch and need speed,
Cython is an excellent option. For this I really must congratulate the
Cython and Pyrex developers.« →
»I wrote a script that compute a distance matrix (O^2) in
Python with Numpy arrays and the same script in Cython. It took me 10
minutes to figure it out how Cython works and I gained a speed up of
550 times !!! Amazing« →
»I would like to report on a successful Cython project.
Successful in the sense that it was much faster than all code written
by my predecessors mainly because the speed scales almost linearly
with the number of cores. Also, the code is shorter and much easier
to read and maintain. [...]
Making it this fast & short & readable & maintainable
would have been pretty hard without Cython.« →
Alex van Houten
»At work, we’ve started using Cython with excellent success.
We rewrote one particular Perl script as Cython and achieved a 600%
speed improvement. As a Perl lover, this was impressive. We still
get all the benefits of Python such as rapid development and clean
object-oriented design patterns but with the speed of C.« →
»The reason that I was interested in Cython was the long
calculation times I encountered while doing a multi-variable
optimization with a function evaluation that involved solving a
differential equation with scipy.integrate.odeint. By simply
replacing the class that contained the differential equation with a
Cython version the calculation time dropped by a factor 5. Not bad
for half a Sunday afternoons work.« →
»I was surprised how simple it was to get it working both
under Windows and Linux. I did not have to mess with make files or
configure the compiles. Cython integrated well with NumPy and SciPy.
This expands the programming tasks you can do with Python
»This is why the Scipy folks keep harping about Cython – it’s
rapidly becoming (or has already become) the lingua franca of exposing
legacy libraries to Python. Their user base has tons of legacy code
or external libraries that they need to interface, and most of the
reason Python has had such a great adoption curve in that space is
because Numpy has made the data portion of that interface easy.
Cython makes the code portion quite painless, as well.« →
Peter Z. Wang
»Added an optional step of compiling fastavro with Cython.
Just doing that, with no Cython specific code reduced the time of
processing 10K records from 2.9sec to 1.7sec. Not bad for that little
»fastavro compiles the Python code without any specific
Cython code. This way on machines that do not have a compiler users
can still use fastavro.
The end result is a package that reads Avro faster than Java
and supports both Python 2 and Python 3. Using Cython and a little bit
of work th[is] was achieved without too much effort.« →
»... the binding needed to be rewritten, mainly because the
current binding is directly written in C++ and is a maintenance
nightmare. This new binding is written in Cython« →
Code generation via Cython allows the production of smaller and more maintainable bindings, including increased compatibility with all supported Python releases without additional burden for NEST developers.
This approach resulted in a reduction of the code footprint of around 50% and a significant increase in the cohesiveness of the code related to the Python bindings: whereas previously seven core files and 22 additional files were involved, the new approach requires merely two core files. The new implementation also removes the compile-time dependency on NumPy and provides numerous additional maintainability benefits by reducing complexity and increasing comprehensibility of the code. The re-write of the build system also resulted in a 50% reduction of code, and resolved multiple issues with its usability and robustness.
In conclusion, we hope that through a more widespread use of Cython, neuroscientific software developers will be able to focus their creative energy on refining their algorithms and implementing new features, instead of working to pay off the interest on the accumulating technical debt.
Yury V. Zaytsev and Abigail Morrison
The Cython version took about 30 minutes to write, and it runs just as fast as the C code — because, why wouldn’t it? It *is* C code, really, with just some syntactic sugar. And you don’t even have to learn or think about a foreign, complicated C API…You just, write C. Or C++ — although that’s a little more awkward. Both the Cython version and the C version are about 70x faster than the pure Python version, which uses Numpy arrays.
I love this project. Fantastic way to write Python bindings for native libs or speed up computationally intensive code without having to write C yourself.
I use a lot of pyrex/cython to bind to libraries - it's so much faster to code in python. It's been a huge boon. Having used swig, hand writing wrappers, and pyrex before i can say i much prefer cython.
Thank you for the hard work.
more ...less ...