Cython

Cython is a programming language that aims to be a of the, designed to give -like performance with code that is written mostly in Python with optional additional C-inspired syntax.

Cython is a that is typically used to generate CPython extension modules. Annotated python-like code is compiled to C or then automatically, producing extension modules that can be loaded and used by regular Python code using the import statement, but with significantly less computational overhead at. Cython also facilitates wrapping independent C or C++ code into python-importable modules.

Cython is written in Python and C and works on, , and , producing source files compatible with CPython 2.6, 2.7, and 3.3 through 3.7.

Cython 3.0.0 is in development.

Design
Cython works by producing a standard Python module. However, the behavior differs from standard Python in that the module code, originally written in Python, is translated into C. While the resulting code is fast, it makes many calls into the CPython interpreter and CPython standard libraries to perform actual work. Choosing this arrangement saved considerably on Cython's development time, but modules have a dependency on the Python interpreter and standard library.

Although most of the code is C-based, a small stub loader written in interpreted Python is usually required (unless the goal is to create a loader written entirely in C, which may involve work with the undocumented internals of CPython). However, this is not a major problem due to the presence of the Python interpreter.

Cython has a for invoking /  and the ability to declare the  of subroutine parameters and results, local variables, and class attributes.

A Cython program that implements the same algorithm as a corresponding Python program may consume fewer computing resources such as core memory and processing cycles due to differences between the CPython and Cython execution models. A basic Python program is loaded and executed by the CPython, so both the runtime and the program itself consume computing resources. A Cython program is compiled to C code, which is further compiled to machine code, so the virtual machine is used only briefly when the program is loaded.

Cython employs:


 * Optimistic optimizations
 * Type inference (optional)
 * Low overhead in control structures
 * Low function call overhead

Performance depends both on what C code is generated by Cython and how that code is compiled by the C compiler.

History
Cython is a derivative of the, and supports more features and optimizations than Pyrex. Cython was from Pyrex in 2007 by developers of the  computer algebra package, because they were unhappy with Pyrex's limitations and could not get patches accepted by Pyrex's maintainer Greg Ewing, who envisioned a much smaller scope for his tool than the Sage developers had in mind. They then forked Pyrex as SageX. When they found people were downloading Sage just to get SageX, and developers of other packages (including Stefan Behnel, who maintains the library LXML) were also maintaining forks of Pyrex, SageX was split off the Sage project and merged with cython-lxml to become Cython.

Cython files have a  extension. At its most basic, Cython code looks exactly like Python code. However, whereas standard Python is, in Cython, types can optionally be provided, allowing for improved performance, allowing loops to be converted into C loops where possible. For example:

Example
A sample program for Cython is more complex than in most languages because it interfaces with the Python C API and the   extension building facility. At least three files are required for a basic project:


 * A  file to invoke the   build process that generates the extension module
 * A main python program to load the extension module
 * Cython source file(s)

The following code listings demonstrate the build and launch process:

These commands build and launch the program:

Using in IPython/Jupyter notebook
A more straightforward way to start with Cython is through command-line (or through in-browser python console called Jupyter ):

which gives a 95 times improvement over the pure-python version. More details on the subject in the official quickstart page.

Uses
Cython is particularly popular among scientific users of Python, where it has "the perfect audience" according to Python creator. Of particular note:


 * The  computer algebra system depends on Cython, both for performance and to interface with other libraries.
 * Significant parts of the scientific computing libraries, and  are written in Cython.
 * Some high-traffic websites such as use Cython.

Cython's domain is not limited to just numerical computing. For example, the lxml XML toolkit is written mostly in Cython, and like its predecessor Pyrex, Cython is used to provide Python bindings for many C and C++ libraries such as the messaging library. Cython can also be used to develop s for machines; this feature makes use of the  library.