[Numpy-discussion] iterating over an array

Ralf Juengling juenglin at cs.pdx.edu
Thu Jan 13 10:26:15 CST 2005


I have an application where I cannot avoid (afaikt) 
looping over one array dimension. So I thought it 
might help speeding up the code by setting up the
data in a way so that the dimension to loop over is 
the first dimension. This allows to write

for data in a:
   do sth with data

instead of 

for i in range(len(a)):
   data = a[i]   
   do sth with data

and would save the indexing operation. To my surprise
it didn't make a difference in terms of speed. A
little timing experiment suggests, that the first
version is actually slightly slower than the second:

>>> setup = 'import numarray as na; a = na.arange(2000,shape=(1000,2))'

>>> Timer('for row in a: pass', setup).timeit(number=1000)

>>> Timer('for i in range(len(a)): row=a[i]', setup).timeit(number=1000)

I noticed that the array object does not have a special
method __iter__, so apparently, no attempts have been
made so far to make array iteration fast. Do you think 
it's possible to speed things up by implementing an 
__iter__ method? 

This is high on my wish list and I would help with 
implementing it, appreciating any advice.


More information about the Numpy-discussion mailing list