Cython: Difference between revisions

Content deleted Content added
added FreeBSD to list of supporting o/s's
Tags: Reverted Visual edit
m Bot: http → https
Line 5:
| logo = Cython logo.svg
| developer = Robert Bradshaw, Stefan Behnel, et al.
| released = {{Start date and age|df=yes|2007|07|28}}<ref>{{cite web |url=httphttps://www.behnel.de/cythonEP2008/cython-ep2008.html |type=28 July 2007: official Cython launch |title=The Cython Compiler for C-Extensions in Python |last=Behnel |first=Stefan |work=EuroPython |date=2008 |publisher=Vilnius/Lietuva}}</ref>
| license = [[Apache License 2.0]]
| programming language = [[Python (programming language)|Python]]
Line 14:
}}
 
'''Cython''' ({{IPAc-en|ˈ|s|aɪ|θ|ɒ|n}}) is a [[superset]] of the programming language [[Python (programming language)|Python]], which allows developers to write Python code (with optional, C-inspired syntax extensions) that yields performance comparable to that of [[C (programming language)|C]].<ref>{{cite web |url=httphttps://docs.cython.org/src/quickstart/overview.html |title=Cython - an overview — Cython 0.19.1 documentation |publisher=Docs.cython.org |access-date=21 July 2013}}</ref><ref>{{cite book |last1=Smith |first1=Kurt |title=Cython: A Guide for Python Programmers |date=2015 |publisher=[[O'Reilly Media]] |isbn=978-1-4919-0155-7 |url=httphttps://shop.oreilly.com/product/0636920033431.do}}</ref>
 
Cython is a [[compiled language]] that is typically used to generate [[CPython]] extension modules. Annotated Python-like code is compiled to C and then automatically [[wrapper library|wrapped in interface code]], producing extension modules that can be loaded and used by regular Python code using the import statement, but with significantly less computational overhead at [[Run time (program lifecycle phase)|run time]]. Cython also facilitates wrapping independent C or C++ code into python-importable modules.
Line 29:
Cython has a [[foreign function interface]] for invoking [[C (programming language)|C]]/[[C++]] [[subroutine|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.<ref>{{cite web |last=Oliphant |first=Travis |date=20 June 2011 |url=httphttps://technicaldiscovery.blogspot.com/2011/06/speeding-up-python-numpy-cython-and.html |title=Technical Discovery: Speeding up Python (NumPy, Cython, and Weave) |publisher=Technicaldiscovery.blogspot.com |access-date=21 July 2013}}</ref><ref>{{cite journal |last1=Behnel |first1=Stefan |last2=Bradshaw |first2=Robert |last3=Citro |first3=Craig |last4=Dalcin |first4=Lisandro |last5=Seljebotn |first5=Dag Sverre |last6=Smith |first6=Kurt |year=2011 |title=Cython: The Best of Both Worlds |journal=Computing in Science and Engineering |volume=13 |issue=2 |pages=31–39 |doi=10.1109/MCSE.2010.118 |bibcode=2011CSE....13b..31B |s2cid=14292107 |url=httphttps://research.google.com/pubs/pub36727.html|hdl=11336/13103 |hdl-access=free }}
</ref><ref name="scipy09">{{cite book |last=Seljebot |first=Dag Sverre |title=Proceedings of the 8th Python in Science Conference |chapter=Fast numerical computations with Cython |year=2009 |pages=15–22 |doi=10.25080/GTCA8577 |chapter-url=http://conference.scipy.org/proceedings/SciPy2009/paper_2|doi-access=free }}</ref><ref>{{cite journal |last1=Wilbers |first1=I. |last2=Langtangen |first2=H. P. |last3=Ødegård |first3=Å. |year=2009 |title=Using Cython to Speed up Numerical Python Programs |journal=Proceedings of MekIT'09 |pages=495–512 |url=http://simula.no/research/sc/publications/Simula.SC.578/simula_pdf_file |access-date=14 June 2011 |format=PDF |editor1-first=B. |editor1-last=Skallerud |editor2-first=H. I. |editor2-last=Andersson |archive-date=4 January 2017 |archive-url=https://web.archive.org/web/20170104170007/http://simula.no/research/sc/publications/Simula.SC.578/simula_pdf_file |url-status=dead }}</ref>
 
Line 36:
* [[Type inference]] (optional)
* Low overhead in control structures
* Low function call overhead<ref>{{cite web |url=http://telecom.inescporto.pt/~gjc/pybindgen-benchmarks/ |title=wrapper benchmarks for several Python wrapper generators (except Cython) |access-date=28 May 2010 |archive-url=https://web.archive.org/web/20150404154630/http://telecom.inescporto.pt/~gjc/pybindgen-benchmarks/ |archive-date=4 April 2015 |url-status=dead}}</ref><ref>{{cite web |url=httphttps://behnel.de/cycppbench/ |title=wrapper benchmarks for Cython, Boost.Python and PyBindGen |access-date=28 May 2010 |archive-url=https://web.archive.org/web/20160303224104/http://www.behnel.de/cycppbench/ |archive-date=3 March 2016 |url-status=dead}}</ref>
 
Performance depends both on what C code is generated by Cython and how that code is compiled by the C compiler.<ref>{{cite web |title=Cython: C-Extensions for Python |url=http://cython.org/index.html |accessdate=22 November 2015}}</ref>
 
== History ==
Cython is a derivative of the [[Pyrex (programming language)|Pyrex language]], but it supports more features and optimizations than Pyrex.<ref>{{cite web |url=https://github.com/cython/cython/wiki/DifferencesFromPyrex |title=Differences between Cython and Pyrex|website=[[GitHub]] }}</ref><ref>{{cite web |last=Ewing |first=Greg |url=httphttps://mail.python.org/pipermail/python-dev/2011-March/109642.html |title=Re: VM and Language summit info for those not at Pycon (and those that are!)|type=Message to the electronic mailing-list <code>python-dev</code> |date=21 March 2011 |access-date=5 May 2011}}</ref> Cython was [[Fork (software development)|forked]] from Pyrex in 2007 by developers of the [[SageMath|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 {{proper name|LXML}}) were also maintaining forks of Pyrex, SageX was split off the Sage project and merged with <code>{{proper name|cython-lxml}}</code> to become Cython.<ref>{{cite web |author=Says Sage and Cython developer Robert Bradshaw at the Sage Days 29 conference |url=https://www.youtube.com/watch?v=osjSS2Rrvm0 |archive-url=https://ghostarchive.org/varchive/youtube/20211221/osjSS2Rrvm0 |archive-date=21 December 2021 |url-status=live |title=Cython: Past, Present and Future |via=[[YouTube]] |date=22 March 2011 |access-date=5 May 2011}}{{cbignore}}</ref>
 
Cython files have a <code>.pyx</code> extension. At its most basic, Cython code looks exactly like Python code. However, whereas standard Python is [[dynamic typing|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:
Line 153:
 
== Uses ==
Cython is particularly popular among scientific users of Python,<ref name="scipy09" /><ref>{{cite web |title=inSCIght: The Scientific Computing Podcast |url=http://inscight.org/2011/03/31/episode_/ |type=Episode 6 |access-date=29 May 2011 |archive-url=https://web.archive.org/web/20141010032300/http://inscight.org/2011/03/31/episode_/ |archive-date=10 October 2014 |url-status=dead}}</ref><ref>{{cite journal |last1=Millman |first1=Jarrod |last2=Aivazis |first2=Michael |year=2011 |title=Python for Scientists and Engineers |journal=[[Computing in Science and Engineering]] |volume=13 |issue=2 |pages=9–12 |doi=10.1109/MCSE.2011.36 |bibcode=2011CSE....13b...9M |url=https://escholarship.org/uc/item/93s2v2s7}}</ref> where it has "the perfect audience" according to Python creator [[Guido van Rossum]].<ref>{{cite web |author=Guido Van Rossum |url=httphttps://mail.python.org/pipermail/python-dev/2011-March/109634.html |title=Re: VM and Language summit info for those not at Pycon (and those that are!) |type=Message to the electronic mailing-list <code>python-dev</code>|date=21 March 2011 |access-date=5 May 2011}}</ref> Of particular note:
 
* The [[free software]] [[SageMath]] computer algebra system depends on Cython, both for performance and to interface with other libraries.<ref>{{cite book |last1=Erocal |first1=Burcin |last2=Stein |first2=William |title=Mathematical Software – ICMS 2010 |chapter=The Sage Project: Unifying Free Mathematical Software to Create a Viable Alternative to Magma, Maple, Mathematica and MATLAB |year=2010 |volume=6327 |pages=12–27 |publisher=Springer Berlin / Heidelberg |doi=10.1007/978-3-642-15582-6_4 |url=http://wstein.org/papers/icms/icms_2010.pdf |series=Lecture Notes in Computer Science |isbn=978-3-642-15581-9 |citeseerx=10.1.1.172.624}}</ref>
Line 159:
* Some high-traffic websites such as [[Quora]] use Cython.{{better source needed|date=October 2018}}<ref>{{cite web |url=https://www.quora.com/Is-Quora-still-running-on-PyPy/answer/Alex-Yakunin |title=Is Quora still running on PyPy?}}</ref>
 
Cython's ___domain is not limited to just numerical computing. For example, the {{proper name|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]].<ref>{{cite web |url=httphttps://www.zeromq.org/bindings:python |title=ØMQ: Python binding}}</ref> Cython can also be used to develop [[parallel program]]s for [[multi-core processor]] machines; this feature makes use of the [[OpenMP]] library.
 
== See also ==