[Numpy-discussion] Slow element-by-element access?

Travis Oliphant oliphant@enthought....
Mon Nov 22 11:42:03 CST 2010

Basically, indexing in Python is a little slower, the number of things indexing can do is more varied, and more to the point, the objects returned from arrays are NumPy scalars (which have math which is not optimized). 

If you do element-by-element indexing, it's generally best to use Python lists (this has always been true, even with Numeric).   The item method on NumPy arrays will speed up these kind of loops:

from numpy import arange
from time import clock

N = 100000
a = arange(N)
b = arange(N)
al = a.tolist()
bl = b.tolist()
t = clock()
for i in range(1, N-1):
tpass = clock() - t
t = clock()
ai = a.item
for i in range(1, N-1):
    val = ai(i) - t*(ai(i+1) - 2*ai(i) + ai(i-1)) + ai(i)*ai(i)*t
    b.itemset(i, val % (2**31))
tfast = clock() - t
t = clock()
for i in range(1, N-1):
    val = a[i] - t*(a[i+1] - 2*a[i] + a[i-1]) + a[i]*a[i]*t
    b[i] = val % (2**31)
tslow = clock() - t

t = clock()
for i in range(1, N-1):
    val = al[i] - t*(al[i+1] - 2*al[i] + al[i-1]) + al[i]*al[i]*t
    bl[i] = val % (2**31)
tlist = clock() - t

print tpass, tfast, tslow, tlist

On my system, the list version is the fastest, while the itemset method is about 10x faster than the full indexing approach.   The item method not only does faster indexing, but it also returns Python scalars rather than NumPy scalars. 


More information about the NumPy-Discussion mailing list