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?
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.
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
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With