[Numpy-discussion] Square All Elements Of A Matrix

Fernando Perez Fernando.Perez at colorado.edu
Wed May 11 13:35:26 EDT 2005


andrea_gavana at tin.it wrote:
> Hello NG,
> 
>     in one script, I generated a matrix "dx" which has a (200000, 10) shape.
> Well, everything is working fine (matrix multiplication, sum, exponentiation
> etc...), but I'm having problems in squaring all elements of this matrix.
> 
> Suppose I have:
> 
> import Numeric
> import random
> 
> dx = Numeric.ones((200000,10), Numeric.Float0, savespace=1)
> dx[:,:] = random.random()
> 
> td1 = dx*2.0   ---> This takes about 0.2 seconds
> td2 = dx**2.0 ---> This takes about 6 seconds
> 
> Why this difference? And why it is so slow the exponentiation? I have tried
> the same operation in Matlab and it was almost instantaneous. I don't want
> to stick with Matlab, noting the great job the Numeric developers have done.
> 
> Is there something that I am missing?

I'm not sure what the problem may be, but I certainly don't see your results:

In [22]: dx=RandomArray.random((200000, 10)).astype(Numeric.Float0)

In [23]: time td1 = dx*2.0
CPU times: user 0.20 s, sys: 0.06 s, total: 0.26 s
Wall time: 0.26

In [24]: time td2 = dx**2.0
CPU times: user 0.30 s, sys: 0.06 s, total: 0.36 s
Wall time: 0.36

An increase like this (50% in cpu time) is reasonable for computing a power 
vs. a plain multiplication.  This is using Numeric 23.7 on  a Fedora3 box (2.6 
GHZ P4).  Do you know for a fact that you have enough RAM, and that the 6 
secs. don't come from paging (the arrays aren't that large, but you might have 
other stuff in memory forcing paging).

Cheers,

f




More information about the NumPy-Discussion mailing list