Cython

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

Cython
Cython-logo.svg
Developer(s) Robert Bradshaw, Stefan Behnel, et al.
Initial release 28 July 2007; 16 years ago (2007-07-28)[1]
Stable release 0.24 (4 April 2016; 8 years ago (2016-04-04)) [±][2]
Written in Python, C
Operating system Cross-platform
Type Programming language
License Apache License
Website cython.org

The Cython programming language is a superset of Python with a foreign function interface for invoking C/C++ routines and the ability to declare the static type of subroutine parameters and results, local variables, and class attributes.[3][4]

Overview

Cython is a compiled language that generates CPython extension modules. These extension modules can then be loaded and used by regular Python code using the import statement. Cython is written in Python and works on Windows, Linux, and Mac OS X, producing source files compatible with CPython 2.4 through 3.4.

It works by producing a standard Python module. However, the behavior differs from standard Python in that the module code, originally written in Python, is translated into C. While the resulting code is fast, it makes many calls into the CPython interpreter and CPython standard libraries to perform actual work. Choosing this arrangement saved considerably on Cython's development time, but modules have a dependency on the Python interpreter and standard library.

Although most of the code is C-based, a small stub loader written in interpreted Python is usually required (unless the goal is to create a loader written entirely in C, which may involve work with the undocumented internals of CPython). However, this is not a major problem due to the presence of the Python interpreter.

Sample program

Hello World in Cython

A sample hello world program for Cython is more complex than in most languages because it interfaces with the Python C API and the distutils extension building facility. At least three files are required for a basic project:

  • A setup.py file to invoke the distutils build process that generates the extension module
  • A main python program to load the extension module
  • Cython source file(s)

The following code listings demonstrate the build and launch process:

# hello.pyx - Python Module, this code will be translated to C by Cython.
def say_hello():
    print "Hello World!"
# launch.py - Python stub loader, loads the module that was made by Cython.

# This code is always interpreted, like normal Python.
# It is not compiled to C.

import hello
hello.say_hello()
# setup.py - unnecessary if not redistributing the code, see below
from distutils.core import setup
from Cython.Build import cythonize

setup(name = 'Hello world app',
      ext_modules = cythonize("*.pyx"))

These commands build and launch the program:

$ python setup.py build_ext --inplace
$ python launch.py

History

Cython is a derivative of the Pyrex language, and supports more features and optimizations than Pyrex.[5][6]

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 the XML library LXML) were also maintaining forks of Pyrex, SageX was split off the Sage project and merged with cython-lxml to become Cython.[7]

Example

Cython files have a .pyx extension. At its most basic, Cython code looks exactly like Python code. However, whereas standard Python is 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:

def primes(int kmax):  # The argument will be converted to int or raise a TypeError.
    cdef int n, k, i  # These variables are declared with C types.
    cdef int p[1000]  # Another C type
    result = []  # A Python type
    if kmax > 1000:
        kmax = 1000
    k = 0
    n = 2
    while k < kmax:
        i = 0
        while i < k and n % p[i] != 0:
            i = i + 1
        if i == k:
            p[k] = n
            k = k + 1
            result.append(n)
        n = n + 1
    return result

Static type declarations and performance

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. On the one hand, a basic Python program is loaded and executed by the CPython virtual machine, so both the runtime and the program itself consume computing resources. On the other hand, 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.[8][9][10][11]

Cython employs:

  • Optimistic optimizations
  • Type inference (optional)
  • Low overhead in control structures
  • Low function call overhead[12][13]

Performance depends both on what C code is generated by Cython and how that code is compiled by the C compiler.[14]

Uses

Cython is particularly popular among scientific users of Python,[10][15][16] where it has "the perfect audience" according to Python developer Guido van Rossum.[17] Of particular note:

  • The free software Sage computer algebra system depends on Cython, both for performance and to interface with other libraries.[18]
  • Significant parts of the scientific computing libraries SciPy, pandas and scikit-learn are written in Cython.[19][20]
  • Some high traffic websites such as Quora use Cython.[21]

Cython's domain is not limited to just numerical computing. For example, the 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 like the messaging library ZeroMQ.[22] Cython can also be used to develop parallel programs for multi-core processor machines; this feature makes use of the OpenMP library.

See also

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. Lua error in package.lua at line 80: module 'strict' not found.
  10. 10.0 10.1 Lua error in package.lua at line 80: module 'strict' not found.
  11. Lua error in package.lua at line 80: module 'strict' not found.
  12. Lua error in package.lua at line 80: module 'strict' not found.
  13. Lua error in package.lua at line 80: module 'strict' not found.
  14. Lua error in package.lua at line 80: module 'strict' not found.
  15. Lua error in package.lua at line 80: module 'strict' not found.
  16. Lua error in package.lua at line 80: module 'strict' not found.
  17. Lua error in package.lua at line 80: module 'strict' not found.
  18. Lua error in package.lua at line 80: module 'strict' not found.
  19. Lua error in package.lua at line 80: module 'strict' not found.
  20. Lua error in package.lua at line 80: module 'strict' not found.
  21. Lua error in package.lua at line 80: module 'strict' not found.
  22. Lua error in package.lua at line 80: module 'strict' not found.

External links

  • No URL found. Please specify a URL here or add one to Wikidata.