[Numpy-discussion] iterating over an array
Francesc Altet
faltet at carabos.com
Fri Jan 14 00:29:22 EST 2005
A Dijous 13 Gener 2005 23:57, Chris Barker va escriure:
> >>> setup = 'import Numeric as na; a = na.arange(2000);a.shape=(1000,2)'
> >>> Timer('for i in range(len(a)): row=a[i]', setup).timeit(number=1000)
> 1.97064208984375
> >>> setup = 'import numarray as na; a = na.arange(2000);a.shape=(1000,2)'
> >>> Timer('for i in range(len(a)): row=a[i]', setup).timeit(number=1000)
> 27.220904111862183
>
> yup! that's it. numarray's indexing is SLOW. So it's not an iterator
> issue. Look in the archives of this list for discussion of why
> numarray's generic indexing is slow. A search for "wxPython indexing"
> will probably turn it up.
Well, if you want to really compare generic indexing speed, you can't mix
array creation objects in the process, as your example seems to do.
A pure indexing access test would look like:
>>> setup = 'import numarray as na; a = [i*2 for i in range(2000)]'
>>> Timer('for i in range(len(a)): row=a[i]', setup).timeit(number=1000)
0.48835396766662598 # With Python Lists
>>> setup = 'import Numeric as na; a = na.arange(2000);a.shape=(1000*2,)'
>>> Timer('for i in range(len(a)): row=a[i]', setup).timeit(number=1000)
0.65753912925720215 # With Numeric
>>> setup = 'import numarray as na; a = na.arange(2000);a.shape=(1000*2,)'
>>> Timer('for i in range(len(a)): row=a[i]', setup).timeit(number=1000)
0.89093804359436035 # With numarray
That shows that numarray indexing is slower than Numeric, but not by a large
extent (just a 40%). The real problem with numarray (for Ralf's example) is,
as is already known, array creation time.
Cheers,
--
>OO< Francesc Altet || http://www.carabos.com/
V V Carabos Coop. V. || Who is your data daddy? PyTables
""
More information about the NumPy-Discussion
mailing list