[SciPy-User] Speed up sin/sqrt functions with cython

Nicolau Werneck nwerneck at gmail.com
Wed Jan 12 12:27:00 EST 2011


On Wed, Jan 12, 2011 at 01:05:25PM +0100, g.plantageneto at runbox.com wrote:
> 
> Hi,
> 
> I am using cython to speed up some computations (btw, thanks a lot to the people who gave me suggestions on a previous thread).
> I don't understand how to apply the fast C-coded sin/sqrt functions to a numpy array.
> I can import from math.h like this:
> 
> cdef extern from "math.h"
>     double sin(double)
> 
> but then I can't use this function on a numpy ndarray, obviously.
> 
> Any ideas? It sounds silly to me to write a cycle on ndarray elements.

Hi. When I started using Cython I also thought that was strange, but
that is really how it works. It gets faster than using Numpy because
numpy makes a new loop for each operation, and also stores
intermediate results in new arrays. With Cython (and other tools) you
make a single loop where you calculate complex expressions completely
at each iteration without storing intermediate results in memory.

When you implement a function in Cython you must create this loop over
the array values, and also make sure you use "cdef" in the necessary
variables. You must take care because it is very easy to forget to
declare a variable properly, and although the program will work
correctly, it will be slower. You must also use

@cython.boundscheck(False)
@cython.wraparound(False)

to speed up the array accesses. You can even use C pointers to the
arrays, but that doesn't add much from just switching off these
checks.


Regarding the sin and sqrt, you should not that simply using sin
inside Cython shouldn't accelerate much from e.g. doing a sin(x) on an
array from Python. The speedup is more related to the memory accesses
and number of loops. But you can try to substitute the sin
calculations using memoization, for example, or polynomials. And as I
mentioned previously, you can use the rsqrt instruction in the case of
sqrt calculations. I wrote about that in my blog a few months ago:

http://xor0110.wordpress.com/2010/09/16/using-the-sse-rsqrt-from-python-via-cython/


   ++nic



-- 
Nicolau Werneck <nwerneck at gmail.com>          C3CF E29F 5350 5DAA 3705
http://www.lti.pcs.usp.br/~nwerneck           7B9E D6C4 37BB DA64 6F15
Linux user #460716
"We should continually be striving to transform every art into a science: in the process, we advance the art."
-- Donald Knuth




More information about the SciPy-User mailing list