[SciPy-dev] Indexing array performance
Travis Oliphant
oliphant.travis at ieee.org
Sat Dec 31 13:42:30 CST 2005
Francesc Altet wrote:
>however, the next code seems to be slower in scipy_core:
>
>In [114]: t3 = timeit.Timer('a=empty(shape=10000);a[arange(10000)]','from
>scipy.base import empty, arange')
>
>In [115]: t3.repeat(3,1000)
>Out[115]: [3.5126161575317383, 3.5309510231018066, 3.5558919906616211]
>
>In [116]: t4 = timeit.Timer('a=array(None,shape=10000);a[arange(10000)]','from
>numarray import array, arange')
>
>In [117]: t4.repeat(3,1000)
>Out[117]: [2.0824751853942871, 2.1258058547973633, 2.0946059226989746]
>
>It seems like if the index array feature can be further optimized in
>scipy_core.
>
>
>
I just did some simple tests. It looks like 2-d indexing is quite a bit
faster in scipy_core.
Try these:
t4 =
timeit.Timer('a=array(None,shape=(1000,1000));a[arange(1000),arange(1000)]','from
numarray import array, arange')
t3 =
timeit.Timer('a=empty(shape=(1000,1000));a[arange(1000),arange(1000)]','from
scipy.base import empty, arange')
My results:
>>> t3.repeat(3,100)
[0.18409419059753418, 0.19265508651733398, 0.18711185455322266]
>>> t4.repeat(3,100)
[4.0139532089233398, 3.9884538650512695, 4.405332088470459]
However, as you noticed 1-d indexing is a bit slower. However if you
use flat indexing (which is special-cased) it is faster:
Thus:
t4 = timeit.Timer('a=array(None,shape=10000);a[arange(10000)]','from
numarray import array, arange')
t3 = timeit.Timer('a=empty(shape=10000);a.flat[arange(10000)]','from
scipy.base import empty, arange')
Gives:
>>> t3.repeat(3,100)
[0.18227100372314453, 0.16614699363708496, 0.16269397735595703]
>>> t4.repeat(3,100)
[0.40496301651000977, 0.34369301795959473, 0.3347930908203125]
Thus, I think it might be wise to use the flattened indexing code when
the array is already 1-d. I could add this in automatically.
-Travis
More information about the Scipy-dev
mailing list