Cython

from Wikipedia, the free encyclopedia
Cython
Cython-logo.svg
Basic data
Paradigms : multi-paradigmatic
Publishing year: July 28, 2007
Developer: Robert Bradshaw, Stefan Behnel, et al.
Current  version 0.29.21   (July 8, 2020)
Typing : strong , static , dynamic (" duck typing ")
Influenced by: Python , C , C ++
Operating system : Linux , unix-like system
License : Apache license, version 2.0, Apache license
cython.org

Cython is a universal programming language that is largely compatible with Python . Like these, it supports various programming paradigms such as object-oriented , aspect-oriented and functional programming. The main advantage, however, lies in the translation into the target language C, which allows both high performance compared to the standard Python interpreter and direct interaction with external code written in C, C ++ or Fortran .

The main applications are the connection of external libraries to the CPython - interpreter , as well as the acceleration of Python code. The Cython compiler can also be used to compile pure Python modules.

The generated code is not self-sufficient, but requires a CPython-compatible runtime environment . Both the generation of binary modules ("extension module"), which can be dynamically importloaded into the Python interpreter via the command, and the linker integration of the CPython interpreter into the generated module, so that an executable Program is created ( embedding ).

Development history

Cython is based on Pyrex by Greg Ewing (first published April 4, 2002). In July 2007 the Cython compiler was split off from Pyrex as an independent, openly developed project by Robert Bradshaw and Stefan Behnel. Since then, attempts have been made to keep the two compilers largely compatible, although Cython uses far-reaching optimizations and achieves greater compatibility with Python code. Important differences to Pyrex are described in the Cython documentation.

Origin of name

The name Cython is a suitcase word of the two underlying programming languages: Python and C.

aims

The Cython project has set itself the goal of developing a compiler for normal Python code that, through additional (explicit) static typing, offers the highest possible performance and extensive integration with code written in C, C ++ and Fortran.

Cython is an optimizing compiler, but not in the usual sense of a compiler that generates binary code . Rather, the translation into C code allows many fundamental optimizations and platform adjustments to be left to a C compiler. The Cython compiler can thus limit itself to high-level optimizations at the AST level.

The C code that Cython generates is largely portable in terms of platforms (processor, C compiler and operating system) and CPython versions. Currently (version 0.28.5) the CPython versions 2.6 and 2.7 as well as all from 3.3 on are supported. Tested platforms include macOS , Windows and, above all, various Linux distributions with 32-bit and 64-bit system versions.

properties

The combination of Python and C allows Cython to map a very wide range of requirements. High-level programming with Python data types, language constructs and automatic memory management, as well as programming very close to C with C data types, C functions and manual memory management is possible. Both can be combined as desired and the interaction with pure (uncompiled) Python code is just as natively possible as the interaction with C or C ++ code, which enables a very fine-grained approach when optimizing Cython code. Any part of the code can be written in pure Python, compiled with Cython, optimized with static data types or even ported to C, depending on the requirements. As a result, Cython very well supports the principle that most of the runtime of a program is created in a very small part of the source code , i.e. optimizing a small part of the program can bring a very large gain in performance, while for the majority of the code a high development speed and a low maintenance requirements are more important than the highest possible execution speed.

The programming language achieves language integration between Python and C primarily through data types. Different Python language constructs can be applied to both Python data types and C data types. An example is the 'for' loop, which is a foreach loop in Python, i.e. it can run over any iterable container (e.g. lists or files). In Cython, this loop can also run over C ++ lists, C arrays and subarrays, as well as over pointer sections (slices, e.g. ptr[2:8]for offsets 2–7). In addition, there is an automatic conversion between different Python data types and C data types, both for scalar types (numbers) and for character strings and structured data types (e.g. C struct types and Python dictionaries).

performance

Many of the optimizations that the Cython compiler performs automatically result in a specialization of the generated C code. To do this, the compiler uses explicit static type declarations and (simple) type inference in order to generate specialized C code for the data types used and certain code patterns. A large part of the optimizations in the Cython compiler relate to loops, since a large part of the total runtime usually accumulates here.

Control structures (especially loops) are translated many times faster with Cython and a C compiler than when they are interpreted by CPython. This is due to optimistic optimizations and type inference. This means that Cython-compiled Python code usually runs faster than in CPython 2.6.x even without explicit type declarations, although the relative performance naturally depends on the code in question. However, through the static declaration of data types and the resulting specialization of the C code, it is usually possible to achieve a multiple acceleration. Especially with mathematical calculations, runtime improvements often result by a factor of one hundred to one thousand. In comparison, the typical acceleration through the Python JIT compiler Psyco is around four to a hundred times , with PyPy in selected cases up to twelve times.

The code generated by Cython for functions is optimized for quick unpacking and conversion of call parameters. Therefore, calling native code to Python through a Cython wrapper is generally faster than other wrapper implementations for Python.

application areas

The most important areas of application of Cython are the connection of external libraries to the CPython interpreter, as well as the acceleration of Python code, especially in mathematical calculations and computationally intensive algorithms .

For example, the SageMath computer algebra system is largely based on Cython code. This is used to implement mathematical algorithms as well as to link external codes in C, C ++ and Fortran. Cython also supports a very efficient interaction with NumPy - matrices , which based on these calculations greatly simplified.

The high-performance XML library lxml is largely implemented in Cython. The external C libraries libxml2 and libxslt are linked to the Python interpreter.

Another example from the Cython Core developer environment is the MPI library mpi4py. It connects various MPI implementations to CPython.

In the Python Package Index there is a short list of other libraries that are implemented in Cython.

Related and similar projects

  • Pyrex - predecessor of Cython
  • Unladen Swallow - optimized CPython runtime environment; based on LLVM
  • PyPy - JIT compiler framework and Python runtime that is itself written in a Python dialect (RPython)
  • Psyco - specialized JIT compiler and code optimizer for the CPython runtime environment
  • Shed Skin - statically typed, Python-like programming language that is compiled according to C ++

Individual evidence

  1. Release 0.29.21 . July 8, 2020 (accessed July 9, 2020).
  2. github.com . (accessed on February 2, 2020).
  3. Cython: C-Extensions for Python . (accessed on July 8, 2018).
  4. original versions of the predecessor Pyrex
  5. Compatibility with Python 2 is the declared goal for the future Cython version 1.0 ( Memento of the original from August 10, 2011 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / wiki.cython.org
  6. slices on docs.python.org, accessed on August 2, 2018
  7. gmane.comp.python.cython.devel - pybench comparison of Cython and CPython 2.6.2
  8. gmane.comp.python.cython.devel - Passing a pointer from Python ( Memento of the original from February 14, 2017 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / thread.gmane.org
  9. ^ Psyco - Introduction
  10. Speed ​​of PyPy 1.4 compared to CPython 2.6.2 with / without Psyco
  11. C ++ wrapper benchmarks for various Python wrapper generators (without Cython) ( Memento of the original from April 4, 2015 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / telecom.inescporto.pt
  12. C ++ wrapper benchmarks for Cython, Boost.Python and PyBindGen
  13. List of the Python packages implemented in Cython on PyPI