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

Bruce Sherwood basherwo@ncsu....
Mon Nov 22 00:17:17 CST 2010

A colleague showed me a program using Numeric with Python 2.5 which
ran much faster than the same program using numpy with Python 2.7. I
distilled this down to a simple test case, characterized by a "for"
loop in which he does an element-by-element calculation involving

from numpy import arange # or from Numeric import arange
from time import clock
# Numeric 0.24 seconds; 15 times as fast as numpy
# numpy 3.6 seconds
N = 100000
a = arange(N)
b = arange(N)
t = clock()
for i in range(1,N-1):
tpass = clock()-t
t = clock()
for i in range(1,N-1):
   b[i] = a[i]-t*(a[i+1]-2*a[i]+a[i-1])+a[i]*a[i]*t
t = clock()-t
print t-tpass

His calculation b[i] = a[i]-t*(a[i+1]-2*a[i]+a[i-1])+a[i]*a[i]*t is 15
times faster with Numeric than with numpy.

It is of course the case that he should have done a single array
calculation rather than use a "for" loop, and I've explained that to
him. The array calculation runs at about the same speed in numpy as in

Nevertheless, I'm surprised that the element-by-element calculation is
so very slow, and surely there are situations where individual element
access is important in a numpy calculation. Is this a known issue?
Does it matter? I was unable to find any discussion of this.

Bruce Sherwood

More information about the NumPy-Discussion mailing list