No speed gains from Cython No speed gains from Cython numpy numpy

No speed gains from Cython


Cython can produce an html file to help with this:

cython -a MODULE.py

This shows each line of source code colored white through various shades of yellow. The darker the yellow color, the more dynamic Python behaviour is still being performed on that line. For each line that contains some yellow, you need to add more static typing declarations.

When I'm doing this, I like to split parts of my source code that I'm having trouble with onto many separate lines, one for each expression or operator, to get the most granular view.

Without this, it's easy to overlook some static type declarations of variables, function calls or operators. (e.g. the indexing operator x[y] is still a fully-dynamic Python operation unless you declare otherwise)


Cython doesn't offer automatic performance gains, you have to know its internals and check the generated C code.

In particular if you want to improve loops performances, you have to avoid calling Python functions in them, which you happen to do a lot in this case (all the np. calls are Python calls, slicing, and probably other things).

See this page for general guidelines about performance optimization with Cython (the -a switch really is handy when optimizing) and this one for specificities when optimizing numpy code.


You could definitely speed up your code by using more of Numpy's capabilities.

For instance:

cdef np.ndarray[double, ndim=1] S = np.zeros(dtype = "d", shape = J)cdef int jfor j in xrange(ns):    S += P_i[:,j]

would be much faster and legible as

S = P_i.sum(axis=1)

You also repeat some calculations, which thus take twice more time than necessary. For instance

np.where(data[:,1]==(yr + 72))

could be calculated only once and stored in a variable that you could reuse.

You also perform a lot of reshaping and slicing: it could help to have your variables be in a simpler format from the beginning on. If possible, your code would be much clearer, and optimizations could be much more obvious.