|
particular fast NumPy array access). While thereBog'liq cython cise particular fast NumPy array access). While there
is a subset of syntax that will work both in Pyrex
and Cython, the languages are diverging and one
will in general have to choose one or the other.
For instance, the syntax for calling C++ code is
different in Pyrex and Cython, since this feature
was added long after the fork.
There are other projects that make possible the
inclusion of compiled code in Python (e.g. Weave
and Instant). A comparison of several such tools
can be found in
[comparison]
. Another often used
approach is to implement the core algorithm in
C, C++ or Fortran and then create wrappers
for Python. Such wrappers can be created with
Cython or with more specialized tools such as
SWIG, ctypes, Boost.Python or F2PY. Each tool
has its own flavor. SWIG is able to automatically
wrap C or C++ code while Cython and ctypes
require redeclaration of the functions to wrap.
SWIG and Cython require a compilation stage
which ctypes does not. On the other hand, if one
gets a declaration wrong using ctypes it can result
in unpredictable program crashes without prior
warning. With Boost.Python one implements a
Python module in C++ which – depending on
who you ask – is either a great feature or a great
disadvantage.
Finally, numexpr
1
and Theano
2
are specialized
tools for quickly evaluating numerical expressions
(see below).
To summarize, Cython could be described as a
swiss army knife: It lacks the targeted function-
ality of more specialized tools, but its generality
and versatility allow its application in almost any
situation that requires going beyond pure Python
code.
Cython at a glance
Cython is a programming language based on
Python, that is translated into C/C++ code, and
finally compiled into binary extension modules
that can be loaded into a regular Python ses-
sion. Cython extends the Python language with
explicit type declarations of native C types. One
can annotate attributes and function calls to be
resolved at compile-time (as opposed to runtime).
With the extra information from the annotations,
Cython is able to generate code that sidesteps
most of the usual runtime costs.
The generated code can take advantage of all the
optimizations the C/C++ compiler is aware of
without having to re-implement them as part of
Cython. Cython integrates the C language and
the Python runtime through automatic conver-
sions between Python types and C types, allowing
the programmer to switch between the two with-
out having to do anything by hand. The same
applies when calling into external libraries writ-
ten in C, C++ or Fortran. Accessing them is a
native operation in Cython code, so calling back
and forth between Python code, Cython code and
native library code is trivial.
Of course, if we’re manually annotating every
variable, attribute, and return type with type in-
formation, we might as well be writing C/C++
directly. Here is where Cython’s approach of ex-
tending the Python language really shines. Any-
thing that Cython can’t determine statically is
compiled with the usual Python semantics, mean-
ing that you can selectively speed up only those
|
| |