Commit f9a1e09b authored by Robert Bradshaw's avatar Robert Bradshaw

Integrate test from quickstart.

parent 0cc67f9d
def f(x):
return x**2-x
def integrate_f(a, b, N):
s = 0.0
dx = (b-a)/N
for i in range(N):
s += f(a+i*dx)
return s * dx
def f(x):
return x**2-x
def integrate_f(a, b, N):
s = 0.0
dx = (b-a)/N
for i in range(N):
s += f(a+i*dx)
return s * dx
cdef double f(double x): except? -2:
return x**2-x
def integrate_f(double a, double b, int N):
cdef int i
s = 0.0
dx = (b-a)/N
for i in range(N):
s += f(a+i*dx)
return s * dx
import timeit
import integrate0, integrate1, integrate2
number = 10
py_time = None
for m in ('integrate0', 'integrate1', 'integrate2'):
print m
t = min(timeit.repeat("integrate_f(0.0, 10.0, 10000000)", "from %s import integrate_f" % m, number=number))
if py_time is None:
py_time = t
print " ", t / number, "s"
print " ", py_time / t
...@@ -80,7 +80,7 @@ argument in order to pass it. ...@@ -80,7 +80,7 @@ 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) except? -2: cdef double f(double x) except? -2:
return x**2-x 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
......
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