Cython
Encyclopedia
Cython is a programming language
to simplify writing C
and C++
extension modules for the CPython
Python
runtime. Strictly speaking, Cython syntax is a superset of Python syntax additionally supporting:
Cython compiles to C or C++ code rather than Python, and the result is used as a Python Extension Module or as a stand-alone application embedding the CPython
runtime.
Cython was forked from Pyrex in 2007 by developers of the Sage 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 LXML) were also maintaining forks of Pyrex, SageX was split off the Sage project and merged with cython-lxml to become Cython.
Due to reduced overhead in control structures (especially loops), optimistic optimisations and (limited) type inference, Cython compiled Python code usually executes faster than in the CPython 2.6.x interpreter, although the absolute improvements largely depend on the code. With type declarations, the typical speed-up for numerical/array computations is about 100x-1000x. In comparison, the typical speed-up with Psyco
(Python JIT
compiler) is about 4x-100x.
Cython has been optimised for low call overhead, so a Python call into external C/C++ code through a Cython wrapper is usually faster than with most other wrapping solutions.
, Linux
, and Mac OS X
.
, where it has "the perfect audience" according to Python developer Guido van Rossum
. Of particular note:
Cython's domain is not limited to just numerical computing. For example, the lxml XML toolkit is written mostly in Cython, and Cython is used to provide Pythonic bindings for many C and C++ libraries ranging from from the graphics library OpenGL
to the messaging library ZeroMQ.
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
to simplify writing C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
extension modules for the CPython
CPython
CPython is the default, most-widely used implementation of the Python programming language. It is written in C. In addition to CPython, there are two other production-quality Python implementations: Jython, written in Java, and IronPython, which is written for the Common Language Runtime. There...
Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
runtime. Strictly speaking, Cython syntax is a superset of Python syntax additionally supporting:
- Direct calling of C functions, or C++ functions/methods, from Cython code.
- Strong typing of Cython variables, classes, and class attributes as C types.
Cython compiles to C or C++ code rather than Python, and the result is used as a Python Extension Module or as a stand-alone application embedding the CPython
CPython
CPython is the default, most-widely used implementation of the Python programming language. It is written in C. In addition to CPython, there are two other production-quality Python implementations: Jython, written in Java, and IronPython, which is written for the Common Language Runtime. There...
runtime.
History
Cython is a derivative of the Pyrex language, and supports more features and optimizations than Pyrex.Cython was forked from Pyrex in 2007 by developers of the Sage 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 LXML) were also maintaining forks of Pyrex, SageX was split off the Sage project and merged with cython-lxml to become Cython.
Advantages
The special advantage of this seamless Python/C intermix approach is that existing Python code can be tuned to almost the speed of C by just adding a few static type declarations and by making some adaptations in critical loops - without requiring complicated interface code. The coding speed and the readability of the code remains very Pythonic.Due to reduced overhead in control structures (especially loops), optimistic optimisations and (limited) type inference, Cython compiled Python code usually executes faster than in the CPython 2.6.x interpreter, although the absolute improvements largely depend on the code. With type declarations, the typical speed-up for numerical/array computations is about 100x-1000x. In comparison, the typical speed-up with Psyco
Psyco
Psyco is a specializing just-in-time compiler for Python originally developed by Armin Rigo and further maintained and developed by Christian Tismer....
(Python JIT
Just-in-time compilation
In computing, just-in-time compilation , also known as dynamic translation, is a method to improve the runtime performance of computer programs. Historically, computer programs had two modes of runtime operation, either interpreted or static compilation...
compiler) is about 4x-100x.
Cython has been optimised for low call overhead, so a Python call into external C/C++ code through a Cython wrapper is usually faster than with most other wrapping solutions.
Compatibility
Cython is written in Python and works on WindowsMicrosoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...
, Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...
, and Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...
.
Uses
Cython is particularly popular among scientific users of Python, where it has "the perfect audience" according to Python developer Guido van Rossum
Guido van Rossum
Guido van Rossum is a Dutch computer programmer who is best known as the author of the Python programming language. In the Python community, Van Rossum is known as a "Benevolent Dictator For Life" , meaning that he continues to oversee the Python development process, making decisions where necessary...
. Of particular note:
- The free softwareFree softwareFree software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...
Sage computer algebra system depends on Cython, both for performance and to interface with other libraries. - Significant parts of the scientific and numerical computing libraries SciPySciPySciPy is an open source library of algorithms and mathematical tools for the Python programming language.SciPy contains modules for optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers and other tasks common in science and...
and NumPy are written in Cython.
Cython's domain is not limited to just numerical computing. For example, the lxml XML toolkit is written mostly in Cython, and Cython is used to provide Pythonic bindings for many C and C++ libraries ranging from from the graphics library OpenGL
OpenGL
OpenGL is a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL...
to the messaging library ZeroMQ.
See also
- Unladen SwallowUnladen SwallowUnladen Swallow was an optimization branch of CPython, intended to be fully compatible and significantly faster. It aimed to achieve its goals by supplementing CPython's custom virtual machine with a JIT built using LLVM. The project had stated a goal of a five-times speed improvement over CPython...
, optimised CPython runtime based on LLVM - PyPyPyPyPyPy is a Python interpreter and JIT compiler. PyPy focuses on speed, efficiency and 100% compatibility with the original CPython interpreter.- Details and motivation :...
, JIT compiler framework and runtime environment for Python - PsycoPsycoPsyco is a specializing just-in-time compiler for Python originally developed by Armin Rigo and further maintained and developed by Christian Tismer....
, JIT specialising code optimiser for the CPythonCPythonCPython is the default, most-widely used implementation of the Python programming language. It is written in C. In addition to CPython, there are two other production-quality Python implementations: Jython, written in Java, and IronPython, which is written for the Common Language Runtime. There...
runtime - ShedskinShedskinShed Skin is a Python to C++ programming language compiler. It is experimental, and can translate pure, but implicitly statically typed Python programs into optimized C++...
, static Python-like language that compiles to C++