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

Perry Greenfield perry at stsci.edu
Thu Mar 1 11:09:24 EST 2007


On Mar 1, 2007, at 11:03 AM, 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.

I'm guessing it has to do with the number of elements you are using.  
You have to understand that there is a fair amount of overhead in  
setting up an array operation (e.g., for ufuncs). Typically (I'm  
going from old results here so I may be off significantly) it isn't  
until you have arrays of around 1000 elements that as much times is  
spent doing the operation as it is in setting up for it. So for very  
large arrays (> 10,000 elements) the overhead is insignificant. For  
small arrays (e.g., 50 elements, it's all overhead. In that size  
range, lists will usually be much faster.

Perry Greenfield



More information about the NumPy-Discussion mailing list