[Numpy-discussion] when and where to use numpy arrays vs nested lists

Travis Oliphant oliphant@ee.byu....
Thu Mar 1 11:17:33 CST 2007

Mark P. Miller wrote:

>I've been using Numpy arrays for some work recently.  Just for fun, I 
>compared some "representative" code using Numpy arrays and an object 
>comprised of nested lists to represent my arrays.  To my surprise, the 
>array of nested lists outperformed Numpy in this particular application 
>(in my actual code by 10%, results below are more dramatic).
>Can anyone shed some insight here?  The functions that I use in reality 
>are much more complicated than those listed below, but they are 
>nonetheless representative of the type of thing that I'm doing.

NumPy arrays are also not optimized generic containers like lists are.   
In order to support the fancy indexing operations, simple indexing is a 
bit slower for NumPy arrays.   You don't see the difference until you do 
nested loops like this.   NumPy arrays are not designed to be used 
inside of nested loops.

There are some tricks, however, you can apply in order to speed up usage 
of NumPy arrays inside of nested loops.

1) Use the a.item and a.itemset methods to get and set single elements 
in the array.
2) Use the optional arguments to ufuncs to by-pass two of the slowest 
portions of the ufunc
    set-up code that is usually not needed inside of a loop.

Also, selecting by an array of integers is not a particularly fast thing 
to do (it is in C, but it still has to loop over all the integers and 
pull out the results.  Using getitem twice ([]) as you are doing is 
going to be slower then using the cross-product and using getitem one 
time ([])


More information about the Numpy-discussion mailing list