+.. highlight:: cython
+
.. _early-binding-speed-label:
Early Binding for Speed
However with Cython it is possible to gain significant speed-ups through the
use of 'early binding' programming techniques.
-For example, consider the following (silly) code example::
+For example, consider the following (silly) code example:
+
+.. sourcecode:: cython
cdef class Rectangle:
cdef int x0, y0
:meth:`.area` method contain a lot of Python overhead.
However, in Cython, it is possible to eliminate a lot of this overhead in cases
-where calls occur within Cython code. For example::
+where calls occur within Cython code. For example:
+
+.. sourcecode:: cython
cdef class Rectangle:
cdef int x0, y0
But Cython offers us more simplicity again, by allowing us to declare
dual-access methods - methods that can be efficiently called at C level, but
can also be accessed from pure Python code at the cost of the Python access
-overheads. Consider this code::
+overheads. Consider this code:
+
+.. sourcecode:: cython
cdef class Rectangle:
cdef int x0, y0
rect = Rectangle(x0, y0, x1, y1)
return rect.area()
-.. note::
+.. Note::
in earlier versions of Cython, the :keyword:`cpdef` keyword is
:keyword:`rdef` - but has the same effect).
+.. highlight:: cython
Extension Types
===============
Introduction
-------------
+-------------
As well as creating normal user-defined classes with the Python class
statement, Cython also lets you create new built-in Python types, known as
interface to them.
Attributes
-----------
+-----------
Attributes of an extension type are stored directly in the object's C struct.
The set of attributes is fixed at compile time; you can't add attributes to an
+.. highlight:: cython
+
Interfacing with External C Code
================================
+.. highlight:: cython
+
.. _language-basics-label:
Language Basics
+.. highlight:: cython
+
.. _cython-limitations-label:
*************
+.. highlight:: cython
+
.. _numpy_tute-label:
**************************
=============
To add types we use custom Cython syntax, so we are now breaking Python source
-compatibility. Here's :file:`convolve2.pyx`. *Read the comments!*
-
-.. code-block:: cython
+compatibility. Here's :file:`convolve2.pyx`. *Read the comments!* ::
from __future__ import division
import numpy as np
More information on this syntax [:enhancements/buffer:can be found here].
-Showing the changes needed to produce :file:`convolve3.pyx` only:
-
-.. sourcecode:: cython
+Showing the changes needed to produce :file:`convolve3.pyx` only::
...
def naive_convolve(np.ndarray[DTYPE_t, ndim=2] f, np.ndarray[DTYPE_t, ndim=2] g):
2. Negative indices are checked for and handled correctly. The code above is
explicitly coded so that it doesn't use negative indices, and it
(hopefully) always access within bounds. We can add a decorator to disable
- bounds checking:
-
- .. sourcecode:: cython
+ bounds checking::
...
cimport cython
negative values, then this casting will create a very large positive value
instead and you will attempt to access out-of-bounds values). Casting is done
with a special ``<>``-syntax. The code below is changed to use either
-unsigned ints or casting as appropriate:
-
-.. sourcecode:: cython
+unsigned ints or casting as appropriate::
...
cdef int s, t # changed
More generic code
==================
-It would be possible to do:
-
-.. sourcecode:: cython
+It would be possible to do::
def naive_convolve(object[DTYPE_t, ndim=2] f, ...):
+.. highlight:: cython
+
.. _overview-label:
********
+.. highlight:: cython
+
Differences between Cython and Pyrex
====================================
+.. highlight:: cython
+
.. _sharing-declarations-label:
Sharing Declarations Between Cython Modules
+.. highlight:: cython
+
.. _compilation_label:
****************************
+.. highlight:: cython
+
.. _tutorial_label:
*********
finding prime numbers. You tell it how many primes you want, and it returns
them as a Python list.
-:file:`primes.pyx`: ::
+:file:`primes.pyx`:
+
+.. sourcecode:: cython
+ :linenos:
def primes(int kmax):
cdef int n, k, i
+.. highlight:: cython
+
.. _wrapping-cplusplus-label:
Wrapping C++ Classes in Cython