[SciPy-User] Scipy views and slicing: Can I get a view-slice from only certain elements of an array?

Jacob Biesinger jake.biesinger at gmail.com
Tue Nov 2 05:04:12 EDT 2010


> Why not using a numpy.array object instead of array.array?  Indexing
> with them is much faster than using plain lists:
>
>>>> a = np.arange(1000)
>>>> b = np.arange(1e8)
>>>> timeit b[a]
> 100000 loops, best of 3: 10.4 µs per loop
>>>> l = a.tolist()
>>>> timeit b[l]
> 10000 loops, best of 3: 66.5 µs per loop

Thanks for pointing this out-- using scipy.array instead of lists of
ints shaved my iteration time from 4 minutes to 2.5 minutes.

> NumPy arrays helps saving space too:
>
>>>> sys.getsizeof(l)
> 8072
>>>> a.size*a.itemsize
> 8000   # 72 bytes less, not a lot but better than nothing

My data actually looks more like lots of smallish lists.  It looks
like scipy.array and int lists take up about the same space.
a = [range(10) for i in xrange(1000000)]  # V=223m RES=182m
a = [scipy.array(range(10), dtype=scipy.int32) for i in
xrange(1000000)] # V=275m RES=192m



More information about the SciPy-User mailing list