[Numpy-discussion] Square All Elements Of A Matrix

andrea_gavana at tin.it andrea_gavana at tin.it
Wed May 11 13:40:01 EDT 2005


Hello David

>It's faster to do
>
>import RandomArray
>
>dx = RandomArray.random((200000,10)).astype(Numeric.Float0)
>dx.savespace(1)
>
>(it won't use the same RNG as random, though)

thanks for the tip.

>> td1 = dx*2.0   ---> This takes about 0.2 seconds
>> td2 = dx**2.0 ---> This takes about 6 seconds
>>
>Hmm, I don't see this, with either 23.8 or 24.0b2. What version of
>Numeric are you using, and on what platform?

I have currently Numeric 23.8, with numarray 1.3.1. I am on a Windows XP
machine, with 1 GB RAM and 3.0 GHz. My Python version is 2.3.4.

>dx**2 uses the C 'pow' function on each element (so using 2 or 2.0
>here doesn't make a difference), so may be your platform pow() is
>slow.

Probably that is the reason... but why? Is Windows the problem?

>dx*dx should be faster (it takes 80% of the time that dx**2 does on my
>machine).

On my machine, dx*dx is istantaneous (about 0.1 secons). This are my timings:

import time; start = time.time(); td = dx*dx; print time.time() - start
0.121000051498

import time; start = time.time(); td = dx**2; print time.time() - start
4.367000103

!!!!!!!!

Does anyone have an explanation for this?!?!?

Thank you for your suggestions.

Andrea.





More information about the NumPy-Discussion mailing list