[Numpy-discussion] numpy ndenumerate

Nathan Faggian nathan.faggian@gmail....
Sun Feb 26 22:33:18 CST 2012


Hi,

Is there a good reason for ndenumerate in numpy being slower than
standard indexing?

For example:

---

import numpy as np

def fast_itt(a):
    for index, value in np.ndenumerate(a):
        a[index] += 1

def slow_itt(a):
    for r in range(0, a.shape[0]):
        for c in range(0, a.shape[1]):
            a[r,c] += 1

a = np.zeros((100,100))

%timeit fast_itt(a)
10 loops, best of 3: 25.7 ms per loop

%timeit slow_itt(a)
100 loops, best of 3: 13 ms per loop

---

I appreciate that there are better ways of operating on arrays but
there are many good reasons for permuting through indices and
ndenumerate is a nice way of this...

I am left wondering why it performs badly in this case.

Cheers,

Nathan.


More information about the NumPy-Discussion mailing list