2

Let a and b be two numpy.float arrays of length 1024, defined with

cdef numpy.ndarray a
cdef numpy.ndarray b

I notice that:

cdef int i
for i in range(1024):
    b[i] += a[i]

is considerably slower than:

b += a 

Why?

I really need to be able to loop manually over arrays.

7
  • 2
    You're read and followed docs.cython.org/src/tutorial/numpy.html? I mean I doubt you are going to beat b+=a in terms of speed, but what kind of speeds are you actually getting? Commented May 14, 2015 at 15:39
  • 1
    The latter is still using Numpy's own ufunc loops which can include a number of enhancements depending on how your Numpy was compiled. The former does not, and is further harmed by slow indexing of the ndarray objects; the link @ballsdotballs provided discusses this and how to fix it. Commented May 14, 2015 at 15:43
  • Just a side comment : is cdef float x a 32-bit or 64-bit float? Which cdef type will work with numpy.float32, numpy.float64? I looked at the doc but didn't find precise correspondance. Commented May 14, 2015 at 15:46
  • For future reference, I checked : float <=> numpy.float32, double <=> numpy.float64 = numpy.float Commented May 14, 2015 at 16:22
  • nditer handles loops like this nicely. Commented May 14, 2015 at 18:53

1 Answer 1

3

The difference will be smaller if you tell Cython the data type and the number of dimensions for a and b:

cdef numpy.ndarray[np.float64_t, ndim=1] a, b

Although the difference will be smaller, you won't beat b += a because this is using NumPy's SIMD-boosted functions (which will perform depending if your CPU supports SIMD).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.