Cython
More info
- Type
- Compiler / language extension
- Released
- 2007
- Creator
- Robert Bradshaw, Stefan Behnel and others
- License
- Apache 2.0
- Predecessor
- Pyrex
Write nearly ordinary Python, add type declarations to the five lines that are the bottleneck, compile the lot to C.
It began in 2007 as a fork of Greg Ewing's Pyrex, driven by the needs of the Sage mathematics system. Source is translated to C and built into a normal Python extension module, so the rest of the program never notices. A loop that declares cdef int i skips the entire object layer and often runs a hundred times faster.
The second use is just as important: Cython is a comfortable way to wrap existing C and C++ libraries without hand-writing CPython API glue. NumPy, pandas, scikit-learn and spaCy all have Cython in their hottest code paths. The nogil block additionally releases the GIL, so threads can genuinely run in parallel.