Cython

Cython is a universal programming language that is mostly compatible with Python. Like these, it supports several programming paradigms such as object-oriented, aspect-oriented and functional programming. The main advantage lies in the translation into the target language C, which allows both high performance compared to the standard Python interpreter, as well as a direct interaction with external code allows, which is 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" ) is supported, which can be dynamically loaded via the ' import' command in the Python interpreter, and the linker integration of the CPython interpreter in the generated module, so that an executable program is created ( " embedding ").

History of development

Cython is based on Pyrex by Greg Ewing ( First published on 4.4.2002 ). In July 2007, the Cython compiler by Robert Bradshaw and Stefan Behnel as an independent, open project developed by Pyrex was cleaved. Since then, attempts have been made to keep the two compiler mostly compatible, but Cython uses far more ambitious optimisations and achieves a higher compatibility with Python code. Pyrex important differences are described in the Cython documentation.

Origin of the name

The name is a name Cython Summary of the two programming languages ​​underlying: Python and C. The English pronunciation also refers to the hiss of a snake, an allusion to strength and aggressiveness of Python, which are an optimizing compiler to shame.

Objectives

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

Cython is an optimizing compiler, but not produced in the current sense of a compiler, the binary code. Rather, it allows the translation to C code to leave many basic optimizations and platform adjustments a C compiler. So the Cython compiler can restrict itself to high- level optimizations on AST level.

The C code that Cython generates is largely portable respect platforms (processor, C compiler and operating system) and CPython versions. Currently ( Cython 0.17) are all CPython versions for Python 2.4 support, including Python 3.x. Tested platforms are, among others, MacOS X, Microsoft Windows, and especially different Linux distributions, with 32bit and 64bit versions.

Properties

The combination of Python and C Cython allows a very wide range of needs mapping. It is possible to both high-level programming with Python data types, language constructs and automatic memory management, as well as a very C- oriented programming with C data types, C functions and manual memory management. Both can be combined and interact with pure ( uncompiled ) Python code is as native as possible to interact with C or C code, which allows for the optimization of Cython code a very fine-grained approach. Any parts of the code can even be ported to C, depending on the requirements written in pure Python, compiled with Cython optimized with static data types or. This Cython supports very well the principle that most of the duration of a program is created in a very small part of the source code, ie the optimization of a small part of the program can bring a huge performance gain, while the major part of the code a high speed of development and low maintenance are more important than a maximum execution speed.

The programming language to reach language integration between Python and C mainly about data types. Thus, various Python language constructs can be applied both to Python data types and to C data types. One example is the 'for' loop, which is a foreach loop in Python, so it can run on any iterable container (eg lists or files). In Cython, this loop can also run on C- arrays and sub-arrays, as well as pointer - sections ( slices, eg ptr [ 2:8 ] for the offsets 2-7). In addition, there is an automatic conversion between various Python data types and C data types, both scalars (numbers) as well as strings and structured data types (eg C- struct types, and Python dictionaries ).

Performance

Many of the optimizations which automatically performs the Cython compiler cause a specialization of generated C - code. To this end, the compiler uses explicit static type declarations, and (simple) type inference to generate specialized C code for the data types used and certain code patterns. A lot of the optimizations in the Cython compiler refers to loops because here usually accumulates a large part of the total running time.

Control structures ( particularly loops) are Cython and a C compiler translates to a much faster than if they are interpreted by CPython. This is due to optimistic optimizations and type inference. This Cython - compiled Python code runs without explicit type declarations usually faster than CPython 2.6.x, although the relative performance of course depends on the particular code. Through the static declaration of data types and thereby resulting specialization of the C code to an acceleration many times but can usually achieve. Especially in mathematical calculations, there are often running time improvements by a factor of 100-1000. In comparison, the typical acceleration is by the Python JIT compiler Psyco at about 4x - 100x. , In PyPy in selected cases at up to 12x

The code generated by Cython code for functions optimized for faster extracting and converting call parameters. Therefore, a Python call to native code through a Cython wrapper generally faster than other wrapper implementations for Python.

Areas of application

The most important applications of Cython are the integration of external libraries to the CPython interpreter, as well as the acceleration of Python code, in particular in mathematical calculations and compute-intensive algorithms.

For example, the computer algebra system Sage is based in large part on Cython code. This is used both to implement mathematical algorithms, and for connecting the external code in C, C , and Fortran. Cython also supports a very efficient interaction with NumPy arrays, which greatly simplifies calculations based thereon.

The high-performance XML library lxml is mostly implemented in Cython. The external C libraries libxml2 and libxslt are on the Python interpreter attached.

Another example from the field of Cython -core developers the MPI library is mpi4py. It binds various MPI implementations of CPython.

In the Python Package Index is a brief list of additional libraries that are implemented in Cython.

Related and similar projects

  • Pyrex - predecessor of Cython
  • Unladen Swallow - Optimized CPython runtime, based on LLVM
  • PyPy - JIT compiler framework, and Python runtime environment, even in a Python dialect ( RPython ) is written
  • Psyco - obtain specialized JIT compiler and code optimizer for the CPython runtime environment
  • Shed Skin - Statically typed, Python - like programming language that is compiled to C
211196
de