Commit 0a77e3a9 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Moved the code snippets from the .rst file to the examples directory. Those...

Moved the code snippets from the .rst file to the examples directory. Those code snippets are now tested with the mode "compile".
parent 5fadf79e
cdef double f(double x) except? -2:
return x ** 2 - x
def f(x):
return x ** 2 - x
def integrate_f(a, b, N):
s = 0
dx = (b - a) / N
for i in range(N):
s += f(a + i * dx)
return s * dx
def f(double x):
return x ** 2 - x
def integrate_f(double a, double b, int N):
cdef int i
cdef double s, dx
s = 0
dx = (b - a) / N
for i in range(N):
s += f(a + i * dx)
return s * dx
...@@ -30,35 +30,17 @@ Types are declared via the cdef keyword. ...@@ -30,35 +30,17 @@ Types are declared via the cdef keyword.
Typing Variables Typing Variables
---------------- ----------------
Consider the following pure Python code:: Consider the following pure Python code:
def f(x): .. literalinclude:: ../../examples/quickstart/cythonize/integrate.py
return x**2-x
def integrate_f(a, b, N):
s = 0
dx = (b-a)/N
for i in range(N):
s += f(a+i*dx)
return s * dx
Simply compiling this in Cython merely gives a 35% speedup. This is Simply compiling this in Cython merely gives a 35% speedup. This is
better than nothing, but adding some static types can make a much larger better than nothing, but adding some static types can make a much larger
difference. difference.
With additional type declarations, this might look like:: With additional type declarations, this might look like:
def f(double x):
return x**2-x
def integrate_f(double a, double b, int N): .. literalinclude:: ../../examples/quickstart/cythonize/integrate_cy.pyx
cdef int i
cdef double s, dx
s = 0
dx = (b-a)/N
for i in range(N):
s += f(a+i*dx)
return s * dx
Since the iterator variable ``i`` is typed with C semantics, the for-loop will be compiled Since the iterator variable ``i`` is typed with C semantics, the for-loop will be compiled
to pure C code. Typing ``a``, ``s`` and ``dx`` is important as they are involved to pure C code. Typing ``a``, ``s`` and ``dx`` is important as they are involved
...@@ -78,10 +60,9 @@ and in the call to it, yet a Python ``float`` object must be constructed around ...@@ -78,10 +60,9 @@ and in the call to it, yet a Python ``float`` object must be constructed around
argument in order to pass it. argument in order to pass it.
Therefore Cython provides a syntax for declaring a C-style function, Therefore Cython provides a syntax for declaring a C-style function,
the cdef keyword:: the cdef keyword:
cdef double f(double x) except? -2: .. literalinclude:: ../../examples/quickstart/cythonize/cdef_keyword.pyx
return x**2-x
Some form of except-modifier should usually be added, otherwise Cython Some form of except-modifier should usually be added, otherwise Cython
will not be able to propagate exceptions raised in the function (or a will not be able to propagate exceptions raised in the function (or a
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment