Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up for-loop in Cython

Tags:

python

cython

I am still at the beginning to understand how Cython works. This snippet shows one of the slow parts of my program and I am wondering whether this for loop can be improved. It still pretty much looks like the original Numpy version, but I added the cdef's and int-conversion.

cdef Py_ssize_t i, j
cdef double ii, jj

for ii in np.arange(startx, endx+1, 0.1):
    for jj in np.arange(starty, endy+1, 0.1):
        if my_condition(ii, jj):
            i = <int>ii
            j = <int>jj
            data[i, j] += 1

Do you have any recommendations?

like image 418
HyperCube Avatar asked Oct 14 '25 04:10

HyperCube


2 Answers

Study the cython example in

https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html

That uses nditer to hand out the array elements.

And

https://cython.readthedocs.io/en/stable/src/userguide/memoryviews.html

which demonstrates the use of memoryviews (and C arrays) to rapidly iterate over the values.

Either way your goal is to let cython access the databuffer directly rather than by way of the numpy functions.

like image 118
hpaulj Avatar answered Oct 17 '25 22:10

hpaulj


I had the same problem and found this simple solution.

https://github.com/cython/cython/issues/3310#issuecomment-707252866

for i in range(start, stop, step):
    ...

to this:

i = start - step
for _ in range(((stop - start) + (step - 1))//step):
    i += step
    ...