- Cython
-
- Not to be confused with CPython.
Developer(s) Robert Bradshaw, Stefan Behnel, et al. Initial release 28 July 2007[1] Stable release 0.15.1 (September 20, 2011 [2])
[+/−]Operating system Cross-platform Type Programming language License Apache License Website cython.org 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:
- 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 runtime.
Contents
History
Cython is a derivative of the Pyrex language, and supports more features and optimizations than Pyrex.[3][4]
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.[5]
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.[6] 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.[7][8][9] In comparison, the typical speed-up with Psyco (Python JIT compiler) is about 4x-100x.[10]
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.[11][12]
Compatibility
Cython is written in Python and works on Windows, Linux, and Mac OS X.
Uses
Cython is particularly popular among scientific users of Python ,[8][13][14] where it has "the perfect audience" according to Python developer Guido van Rossum.[15] Of particular note:
- The free software Sage computer algebra system depends on Cython, both for performance and to interface with other libraries.[16]
- Significant parts of the scientific and numerical computing libraries SciPy and NumPy are written in Cython.[17][18]
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[19] to the messaging library ZeroMQ.[20]
See also
- Unladen Swallow, optimised CPython runtime based on LLVM
- PyPy, JIT compiler framework and runtime environment for Python
- Psyco, JIT specialising code optimiser for the CPython runtime
- Shedskin, static Python-like language that compiles to C++
References
- ^ 28th July 2007: official Cython launch, The Cython Compiler for C-Extensions in Python, Dr. Stefan Behnel, EuroPython 2008, Vilnius/Lietuva
- ^ http://cython.org/#download
- ^ 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.
- ^ 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. http://research.google.com/pubs/pub36727.html.
- ^ gmane.comp.python.cython.devel - Passing a pointer from Python
- ^ a b Dag Sverre Seljebot (2009). "Fast numerical computations with Cython". Proceedings of the 8 th Python in Science Conference (SciPy 2009): 15–22. http://conference.scipy.org/proceedings/SciPy2009/paper_2.
- ^ I. Wilbers; H. P. Langtangen; Å. Ødegård (2009). B. Skallerud; H. I. Andersson. ed. "Using Cython to Speed up Numerical Python Programs" (pdf). Proceedings of MekIT'09: 495–512. http://simula.no/research/sc/publications/Simula.SC.578/simula_pdf_file. Retrieved June 14, 2011.
- ^ Psyco - Introduction
- ^ 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. http://wstein.org/papers/icms/icms_2010.pdf.
- ^ SciPy 0.7.2 release notes
- ^ NumPy ticket talking about moving from Pyrex to Cython
- ^ PyOpenGL: The Python OpenGL Binding
- ^ http://www.zeromq.org/bindings:python
External links
Categories:
Wikimedia Foundation. 2010.