Hi all,

I have a snippet borrowed from Dag's Notur talk, which I've annotated
in two ways:

a) Using pure Python decorators, compiler directives and a .pxd.
b) Using "cdef" declarations inside a .pyx file.

I have the feeling these should generate equivalent results, but
approach (a) is a bit slower.  Do you have an explanation?

I attach the source files, and the annotated versions are here:

http://mentat.za.net/refer/integrate_hints.html
http://mentat.za.net/refer/integrate_cy.html

Regards
Stéfan
# This code still runs under Python!

# cython: cdivision=True

from __future__ import division
import cython

@cython.locals(x=cython.float)
def f(x):
    return x*x*x*x - 3 * x

@cython.locals(a=cython.float, b=cython.float,
               N=cython.int, s=cython.float,
               dx=cython.float, i=cython.int)
def integrate_f(a, b, N):
    """Rectangle integration of a function.

    Parameters
    ----------
    a, b : ints
        Interval over which to integrate.
    N : int
        Number of intervals to use in the discretisation.

    """
    s = 0
    dx = (b - a) / N
    for i in range(N):
        s += f(a + i * dx)
    return s * dx

Attachment: integrate_hints.pxd
Description: Binary data

Attachment: integrate_cy.pyx
Description: Binary data

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to