Skip main navigation

£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

Find out more

What is Cython?

What is Cython? In this article we give an overview of Cython, the optimising static compiler and extension language.
© CC-BY-NC-SA 4.0 by CSC - IT Center for Science Ltd.

Cython is an optimising static compiler for Python that also provides its own programming language as a superset for standard Python.

Cython is designed to provide C-like performance for a code that is mostly written in Python by adding only a few C-like declarations to an existing Python code. As such, Cython provides the best of the both Python and C worlds: the good programmer productivity of Python together with the high performance of C. Especially for scientific programs performing a lot of numerical computations, Cython can speed up the execution times more than an order of magnitude. Cython makes it also easy to interact with external C/C++ code.

Cython works by transferring existing Python/Cython code into C code, albeit in a form that is generally not easily readable by humans. The resulting C-code calls functions from Python C application programming interface (API), and thus requires an existing Python compiler and runtime system. The Cython generated code is compiled into a Python module. Normally, this module cannot be used as such, but needs to be imported from a Python program that uses the functionality implemented in Cython.

The main mechanism of how Cython speeds up Python programs is by adding static declarations for variables. Thus, one loses some of the dynamic nature of Python when working with Cython. This works best for fundamental data types (integers, floats, strings) and contiguous arrays (such as NumPy arrays), operations on lists and dictionaries do not normally benefit much from Cython.

In summary, Cython can alleviate the following Python performance bottlenecks discussed in Week 1:

  • Interpretation overhead
  • Unboxing / boxing Python objects
  • Overheads of Python control structures
  • Function call overheads
© CC-BY-NC-SA 4.0 by CSC - IT Center for Science Ltd.
This article is from the free online

Python in High Performance Computing

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education