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

Charles R Harris charlesr.harris@gmail....
Mon Nov 22 00:26:37 CST 2010

On Sun, Nov 21, 2010 at 11:17 PM, Bruce Sherwood <basherwo@ncsu.edu> wrote:

> 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
> arrays:
> 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):
>   pass
> 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
> Numeric.
> 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.
Yes, indexing is known to be slow, although I don't recall the precise
reason for that. Something to do with way integers are handled or some such.
There was some discussion on the list many years ago...

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20101121/7acaece6/attachment.html 

More information about the NumPy-Discussion mailing list