Cython: Cython Is A Programming Language That Aims To Be A Superset of The
Cython: Cython Is A Programming Language That Aims To Be A Superset of The
Cython has 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.
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
virtual machine, 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.[7][8][9][10]
Cython employs:
Optimistic optimizations
Type inference (optional)
Low overhead in control structures
Low function call overhead[11][12]
Performance depends both on what C code is generated by Cython and how that code is compiled by the C
compiler.[13]
History
Cython is a derivative of the Pyrex language, and supports more features and optimizations than
Pyrex.[14][15] 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 the XML library LXML)
were also maintaining forks of Pyrex, SageX was split off the Sage project and merged with cython-
lxml to become Cython.[16]
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[1000] # 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
Example
A sample hello world program for Cython is more complex than in
most languages because it interfaces with the Python C API and the
setuptools extension building facility. At least three files are
required for a basic project:
import hello
hello.say_hello()
In [2]: %%cython
...: def f(n):
...: a = 0
...: for i in range(n):
...: a += i
...: return a
...:
...: cpdef g(int n):
...: cdef long a = 0
...: cdef int i
...: for i in range(n):
...: a += i
...: return a
...:
which gives a 95 times improvement over the pure-python version. More details on the subject in the official
quickstart page.[17]
Uses
Cython is particularly popular among scientific users of Python,[9][18][19] where it has "the perfect audience"
according to Python creator Guido van Rossum.[20] Of particular note:
The free software SageMath computer algebra system depends on Cython, both for
performance and to interface with other libraries.[21]
Significant parts of the scientific computing libraries SciPy, pandas and scikit-learn are written
in Cython.[22][23]
Some high-traffic websites such as Quora use Cython.[24]
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 ZeroMQ.[25] Cython can also be used to develop parallel
programs for multi-core processor machines; this feature makes use of the OpenMP library.
See also
PyPy
Numba
References
1. Behnel, Stefan (2008). "The Cython Compiler for C-Extensions in Python" (http://www.behnel.d
e/cythonEP2008/cython-ep2008.html). EuroPython (28 July 2007: official Cython launch).
Vilnius/Lietuva.
2. "Releases – cython/cython" (https://github.com/cython/cython/releases). Retrieved 12 April
2020 – via GitHub.
3. Behnel, Stefan (12 April 2020). "Cython 3.0 alpha 1 released" (https://mail.python.org/archives/
list/python-announce-list@python.org/thread/NYT7RLW2E2XG7PAQJ3QXNKHIRNL56AUX/).
python-announce-list (Mailing list). Retrieved 12 April 2020.
4. "Cython - an overview — Cython 0.19.1 documentation" (http://docs.cython.org/src/quickstart/o
verview.html). Docs.cython.org. Retrieved 21 July 2013.
5. Smith, Kurt (2015). Cython: A Guide for Python Programmers (http://shop.oreilly.com/product/0
636920033431.do). O'Reilly Media. ISBN 978-1-4919-0155-7.
6. "Support Unicode identifiers · Issue #2601 · cython/cython" (https://github.com/cython/cython/is
sues/2601). GitHub. Retrieved 11 October 2019.
7. Oliphant, Travis (20 June 2011). "Technical Discovery: Speeding up Python (NumPy, Cython,
and Weave)" (http://technicaldiscovery.blogspot.com/2011/06/speeding-up-python-numpy-cyth
on-and.html). Technicaldiscovery.blogspot.com. Retrieved 21 July 2013.
8. Behnel, Stefan; Bradshaw, Robert; Citro, Craig; Dalcin, Lisandro; Seljebotn, Dag Sverre;
Smith, Kurt (2011). "Cython: The Best of Both Worlds" (http://research.google.com/pubs/pub36
727.html). Computing in Science and Engineering. 13 (2): 31–39. doi:10.1109/MCSE.2010.118
(https://doi.org/10.1109%2FMCSE.2010.118).
9. Seljebot, Dag Sverre (2009). "Fast numerical computations with Cython" (http://conference.sci
py.org/proceedings/SciPy2009/paper_2). Proceedings of the 8th Python in Science
Conference (SciPy 2009): 15–22.
10. Wilbers, I.; Langtangen, H. P.; Ødegård, Å. (2009). B. Skallerud; H. I. Andersson (ed.). "Using
Cython to Speed up Numerical Python Programs" (http://simula.no/research/sc/publications/Si
mula.SC.578/simula_pdf_file) (PDF). Proceedings of MekIT'09: 495–512. Retrieved 14 June
2011.
11. "wrapper benchmarks for several Python wrapper generators (except Cython)" (https://web.arc
hive.org/web/20150404154630/http://telecom.inescporto.pt/~gjc/pybindgen-benchmarks/).
Archived from the original (http://telecom.inescporto.pt/~gjc/pybindgen-benchmarks/) on 4 April
2015. Retrieved 28 May 2010.
12. "wrapper benchmarks for Cython, Boost.Python and PyBindGen" (http://behnel.de/cycppbenc
h/).
13. "Cython: C-Extensions for Python" (http://cython.org/index.html). Retrieved 22 November
2015.
14. "Differences between Cython and Pyrex" (https://github.com/cython/cython/wiki/DifferencesFro
mPyrex).
15. Ewing, Greg (21 March 2011). "Re: VM and Language summit info for those not at Pycon (and
those that are!)" (http://mail.python.org/pipermail/python-dev/2011-March/109642.html)
(Message to the electronic mailing-list python-dev). Retrieved 5 May 2011.
16. Says Sage and Cython developer Robert Bradshaw at the Sage Days 29 conference (22
March 2011). "Cython: Past, Present and Future" (https://www.youtube.com/watch?v=osjSS2R
rvm0). youtube.com. Retrieved 5 May 2011.
17. "Building Cython code" (https://cython.readthedocs.io/en/latest/src/quickstart/build.html).
cython.readthedocs.io. Retrieved 24 April 2017.
18. "inSCIght: The Scientific Computing Podcast" (https://web.archive.org/web/20141010032300/h
ttp://inscight.org/2011/03/31/episode_/) (Episode 6). Archived from the original (http://inscight.o
rg/2011/03/31/episode_/) on 10 October 2014. Retrieved 29 May 2011.
19. Millman, Jarrod; Aivazis, Michael (2011). "Python for Scientists and Engineers" (https://eschola
rship.org/uc/item/93s2v2s7). Computing in Science and Engineering. 13 (2): 9–12.
doi:10.1109/MCSE.2011.36 (https://doi.org/10.1109%2FMCSE.2011.36).
20. Guido Van Rossum (21 March 2011). "Re: VM and Language summit info for those not at
Pycon (and those that are!)" (http://mail.python.org/pipermail/python-dev/2011-March/109634.
html) (Message to the electronic mailing-list python-dev). Retrieved 5 May 2011.
21. Erocal, Burcin; Stein, William (2010). The Sage Project: Unifying Free Mathematical Software
to Create a Viable Alternative to Magma, Maple, Mathematica and MATLAB (http://wstein.org/p
apers/icms/icms_2010.pdf) (PDF). Mathematical Software' ICMS 2010. Lecture Notes in
Computer Science. 6327. Springer Berlin / Heidelberg. pp. 12–27. CiteSeerX 10.1.1.172.624
(https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.172.624). doi:10.1007/978-3-642-
15582-6_4 (https://doi.org/10.1007%2F978-3-642-15582-6_4). ISBN 978-3-642-15581-9.
22. "SciPy 0.7.2 release notes" (http://docs.scipy.org/doc/scipy/reference/release.0.7.2.html).
23. Pedregosa, Fabian; Varoquaux, Gaël; Gramfort, Alexandre; Michel, Vincent; Thirion, Bertrand;
Grisel, Olivier; Blondel, Mathieu; Prettenhofer, Peter; Weiss, Ron; Dubourg, Vincent;
Vanderplas, Jake; Passos, Alexandre; Cournapeau, David (2011). "Scikit-learn: Machine
Learning in Python". Journal of Machine Learning Research. 12: 2825–2830.
24. "Is Quora still running on PyPy?" (https://www.quora.com/Is-Quora-still-running-on-PyPy).
25. "ØMQ: Python binding" (http://www.zeromq.org/bindings:python).
External links
Official website (https://cython.org)
Cython (https://github.com/cython) on GitHub
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this
site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia
Foundation, Inc., a non-profit organization.