Cython Essentials

  • Cython is a programming language that blends Python with the static type system of C and C++. It adds a small number of keywords to the Python language to tap into C’s type system.
  • Cython is a compiler that translates Cython source code into efficient C or C++ source code. This source can then be compiled into a Python extension module or a standalone executable.
  • CPython provides a C-level interface into the Python language; the interface is known as the Python/C API. Cython uses this C interface extensively, and therefore Cython depends on CPython.

Two common usecases:

  1. We can start with Python code that needs better performance,
  2. We can start with C (or C++) code that needs an optimized Python interface (Wrapper).

Speed-up

  • Function Call Overhead: Python call slower than C. Moreover, Cython generates highly optimized C code that bypasses some of the slower Python/C API calls. Thus it usually performs better than handwritten C extension.
  • Looping
  • Math Operations: Python is dynamically typed and cannot make any type-based optimizations.
  • Stack Versus Heap Allocation: At the C level, a dynamic Python object is entirely heap allocated. Ex: Python floatobjects are immutable, operations using Python floats involve the creation and destruction of heap-allocated objects. The Cython version can declare all variables to be stack-allocated C doubles. As a rule, stack allocation is much faster than heap allocation.

Wrapping

double cfib(int n);

cdef extern from "cfib.h":
    double cfib(int n)
def fib(n):
    """Returns the nth Fibonacci number."""
    return cfib(n)
  • The interface for our function is incfib.h.We provide the cfib.h header filename in the cdef extern from statement, and we declare the cfib function’s signature in the block’s indented body. After the cdef extern block, we define a fib Python wrapper function, which calls cfib and returns its result.
  • A hand-written wrapper would require several dozen lines of C code, and detailed knowledge of the Python/C API.
  • The type conversion code can be generated automatically for simple types. In future chapters, we will see how Cython helps us wrap arbitrarily complex data structures, classes, functions, and methods.
  • Sage uses Cython extensively to speed up Python-centric algorithms.

results matching ""

    No results matching ""