[SciPy-User] help speeding up a Runge-Kuta algorithm (cython, f2py, ...)

Sturla Molden sturla at molden.no
Sat Aug 4 17:49:29 EDT 2012


Den 04.08.2012 21:35, skrev Jonathan Stickel:
> I am sure properly coded Cython is great, but I really struggled when I
> tried to use it.

I uses np.ndarray declarations and array expressions. Those will be 
slow. To write fast and numpyonic Cython, use typed memoryviews instead, 
and write out all loops. I.e. there is no support for array expressions 
with these yet. And unfortunately there is a huge lack of documentation 
on how to use typed memoryviews efficiently.

Here is an example code of how to use Cython as a Fortran killer:

https://github.com/sturlamolden/memview_benchmarks/blob/master/memview.pyx

In this case, the performance with -O2 was just 2.2% slower than "plain 
C" with pointer arithmetics.

It is possible to write very fast array code with Cython, but you must 
do it right.

For comparison, this is very slow:

https://github.com/sturlamolden/memview_benchmarks/blob/master/cythonized_numpy_2b.pyx

What this mean is this: For anything but trivial code, the NumPy syntax 
is just too slow and should be avoided!

I breiefly looked at the Cython code posted in this thread, and it 
suffers form all these issues.


Sturla












More information about the SciPy-User mailing list