[Numpy-discussion] treating numpy arrays like lists is slow]

Jeff Whitaker jswhit at fastmail.fm
Fri Feb 3 19:08:02 EST 2006


Travis Oliphant wrote:
> Jeff Whitaker wrote:
>
>>
>> Hi:
>>
>> I've noticed that code like this is really slow in numpy (0.9.4):
>>
>> import numpy as NP
>> a = NP.ones(10000,'d')
>> a = [2.*a1 for a1 in a]
>>
>>
>> the last line takes 0.17 seconds on my G5, while for Numeric and 
>> numarray it takes only 0.01.  Anyone know the reason for this?
>>
> We could actually change this right now, before the introduction of 
> scalar math by using the standard float table for the corresponding 
> array scalars.   The only reason I didn't do this initially was that I 
> wanted consistency in behavior for "division-by-zero" between arrays 
> and scalars.
> Using the Python float math you will get divide-by-zero errors whereas 
> you don't (unless you ask for them), with numpy arrays.
>
> Thus, current scalars are treated as 0-d arrays in the internals and 
> go through the entire ufunc machinery for every operation. 
> Now, the real question is why are you doing this?  Using arrays in 
> this way defeats their purpose :-)
>
> What is wrong with 2*a?  Now, of course there will be situations that 
> require this.
>
> -Travis
>
Travis:  Of course I know this is a dumb thing to do - but sometimes it 
does happen that a function that expects a list actually gets a rank-1 
array.  The workaround in that case is to just pass it a.tolist() 
instead of a.

-Jeff

-- 
Jeffrey S. Whitaker         Phone  : (303)497-6313
Meteorologist               FAX    : (303)497-6449
NOAA/OAR/PSD  R/PSD1        Email  : Jeffrey.S.Whitaker at noaa.gov
325 Broadway                Office : Skaggs Research Cntr 1D-124
Boulder, CO, USA 80303-3328 Web    : http://tinyurl.com/5telg




-- 
Jeffrey S. Whitaker         Phone  : (303)497-6313
Meteorologist               FAX    : (303)497-6449
NOAA/OAR/PSD  R/PSD1        Email  : Jeffrey.S.Whitaker at noaa.gov
325 Broadway                Office : Skaggs Research Cntr 1D-124
Boulder, CO, USA 80303-3328 Web    : http://tinyurl.com/5telg





More information about the NumPy-Discussion mailing list