- Not to be confused with CPython.
|Developer(s)||Robert Bradshaw, Stefan Behnel, et al.|
|Initial release||28 July 2007|
|Stable release||0.19.1 (11 May 2013) [±]|
The Cython programming language is a superset of Python with a foreign function interface for invoking C/C++ routines and the ability to declare the static type of subroutine parameters and results, local variables, and class attributes.
Cython is a compiled language that generates CPython extension modules. These extension modules can then be loaded and used by regular Python code using the import statement. Cython is written in Python and works on Windows, Linux, and Mac OS X, producing source files compatible with Python 2.4 through 3.3.
Cython has an unusually involved hello world program because it interfaces with the Python C API and the
distutils extension building facility. At least three files are required for a basic project:
setup.pyfile to invoke the
distutilsbuild 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.
# hello.pyx def say_hello(): print "Hello World!"
# launch.py import hello hello.say_hello()
# setup.py from distutils.core import setup from Cython.Build import cythonize setup(name = 'Hello world app', ext_modules = cythonize("*.pyx"))
These commands build and launch the program
$ python setup.py build_ext --inplace $ python launch.py
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.
Cython files have a
.pyx extension. At its most basic, Cython code looks exactly like Python code. However, whereas standard Python is dynamically typed, in Cython, types can optionally be provided, allowing for improved performance, allowing loops to be converted into C loops where possible. For example:
def primes(int kmax): # The argument will be converted to int or raise a TypeError. cdef int n, k, i # These variables are declared with C types. cdef int p # Another C type result =  # A Python type if kmax > 1000: kmax = 1000 k = 0 n = 2 while k < kmax: i = 0 while i < k and n % p[i] != 0: i = i + 1 if i == k: p[k] = n k = k + 1 result.append(n) n = n + 1 return result
Static type declarations and Performance
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. On the one hand, a basic Python program is loaded and executed by the CPython virtual machine, so both the runtime and the program itself consume computing resources. On the other hand, 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.
- Optimistic optimizations
- Type inference (optional)
- Low overhead in control structures
- Low function call overhead
Since C is the intermediate language, performance will depend on the C compiler.
- The free software Sage computer algebra system depends on Cython, both for performance and to interface with other libraries.
- Significant parts of the scientific computing libraries SciPy and scikit-learn 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 like the messaging library ZeroMQ. Cython can also be used to develop parallel programs for multicore machines, this feature makes use of the OpenMP library.
- 28th July 2007: official Cython launch, The Cython Compiler for C-Extensions in Python, Dr. Stefan Behnel, EuroPython 2008, Vilnius/Lietuva
- Cython - an overview — Cython 0.19.1 documentation. Docs.cython.org. Retrieved on 2013-07-21.
- Differences between Cython and Pyrex
- Greg Ewing, Re: VM and Language summit info for those not at Pycon (and those that are!). Message to the electronic mailing-list python-dev, 21 March 2011. Retrieved 5 May 2011.
- Says Sage and Cython developer Robert Bradshaw at the Sage Days 29 conference. Cython: Past, Present and Future, youtube.com, 22 March 2011. Retrieved 5 May 2011.
- Oliphant, Travis. (2011-06-20) Technical Discovery: Speeding up Python (NumPy, Cython, and Weave). Technicaldiscovery.blogspot.com. Retrieved on 2013-07-21.
- Stefan Behnel, Robert Bradshaw, Craig Citro, Lisandro Dalcin, Dag Sverre Seljebotn, Kurt Smith (2011). "Cython: The Best of Both Worlds". Computing in Science and Engineering 13 (2): 31–39. doi:10.1109/MCSE.2010.118.
- Dag Sverre Seljebot (2009). "Fast numerical computations with Cython". Proceedings of the 8 th Python in Science Conference (SciPy 2009): 15–22.
- I. Wilbers; H. P. Langtangen; Å. Ødegård (2009). "Using Cython to Speed up Numerical Python Programs" (PDF). In B. Skallerud; H. I. Andersson. Proceedings of MekIT'09: 495–512. Retrieved June 14, 2011.
- wrapper benchmarks for several Python wrapper generators (except Cython)
- wrapper benchmarks for Cython, Boost.Python and PyBindGen
- inSCIght: The Scientific Computing Podcast, Episode 6
- Jarrod Millman and Michael Aivazis (2011). "Python for Scientists and Engineers". Computing in Science and Engineering 13 (2): 9–12. doi:10.1109/MCSE.2011.36.
- Who also criticizes Cython: Re: VM and Language summit info for those not at Pycon (and those that are!). Message to the electronic mailing-list python-dev, 21 March 2011. Retrieved 5 May 2011.
- Burcin Erocal and William Stein (2010). "The Sage Project: Unifying Free Mathematical Software to Create a Viable Alternative to Magma, Maple, Mathematica and MATLAB". Mathematical Software‚ ICMS 2010 (Springer Berlin / Heidelberg) 6327: 12–27. doi:10.1007/978-3-642-15582-6_4.
- SciPy 0.7.2 release notes
- Fabian Pedregosa; Gaël Varoquaux; Alexandre Gramfort; Vincent Michel; Bertrand Thirion; Olivier Grisel; Mathieu Blondel; Peter Prettenhofer; Ron Weiss; Vincent Dubourg; Jake Vanderplas; Alexandre Passos; David Cournapeau (2011). "Scikit-learn: Machine Learning in Python". Journal of Machine Learning Research 12: 2825–2830.