[SciPy-user] conj and element-by-element multiplication
David M. Cooke
cookedm at physics.mcmaster.ca
Fri May 20 22:23:30 EDT 2005
On May 20, 2005, at 18:35, Ryan Krauss wrote:
> I am running Numeric 24.02b on python 2.3.4. I am using Fedora
> Core 3 and kde.
>
> I have modified the norm2 function to include printing both times:
>
> def norm2(x):
> """compute |x|^2 = x*conjugate(x)"""
> if iscomplexobj(x):
> t1=time.time()
> mat1=x.real**2 + x.imag**2
> t2=time.time()
> mat2=multiply(x.real,x.real) + multiply(x.imag,x.imag)
> t3=time.time()
> print('pow time='+str(t2-t1))
> print('multiply time='+str(t3-t2))
> return mat2
> else:
> return multiply(x,x)
>
> and get these times printed to the screen:
> pow time=0.00401091575623
> multiply time=0.000787019729614
> pow time=0.00442790985107
> multiply time=0.000679016113281
> pow time=0.0130319595337
> multiply time=0.000170946121216
> pow time=0.00465989112854
> multiply time=0.000912189483643
> pow time=0.00434708595276
> multiply time=0.000770807266235
> pow time=0.00153613090515
> multiply time=0.000103950500488
>
> Most of the calls are with 4200x3 matrices. The last one is with a
> 4200x1 vector (I think).
Curious. One more question: are these Complex32 matrices? I.e., are
they matrices using C floats (single precision), as opposed to C
doubles (double precision)? The reason is that if x is a vector of
floats, x**2 upcasts x to doubles first (b/c 2 is an int -- ints are
cast to doubles), so that kills the speed, and I can get timings like
this. This isn't a good behaviour, and I'm going to see what I can do
to fix this for the next version of Numeric.
> David M. Cooke wrote:
>> Ryan Krauss <ryanfedora at comcast.net> writes:
>>> No, I am running Linux.
>> Odd; I do too, and don't see this. glibc's pow doesn't have the
>> same problem as Window's does. x**2 is at most 50% slower, not
>> 10x. Which version of Numeric and python are you using, and which
>> Linux distribution? (Numeric's version you can get from
>> Numeric.__version__) Ideally, x**2 should run in the same time as
>> x*x You're using scipy's multiply, which is the same (I think) as
>> the multiply in Numeric 24.0b2, which are different from 23.8 and
>> earlier (which does a time-wasting postcheck of the array for
>> infinite values).
>>> David M. Cooke wrote: Ryan Krauss <ryanfedora at comcast.net>
>>> writes: Thanks David, that seems like the right answer. But to
>>> get the speed benefit you mention, I need to use multiply
>>> (x.real,x.real) instead of x.real**2 - same for imag. There is
>>> about a factor of 10 time difference between the two. So my norm2
>>> function now looks like this: Are you on Windows? The pow()
>>> function there, well, sucks big time. On other platforms the
>>> two are pretty close in speed. I'm working on a better version of
>>> the power ufunc for Numeric, so hopefully in a later version this
>>> will be fixed (or rather, worked around).
--
|>|\/|<
/------------------------------------------------------------------\
|David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca
More information about the SciPy-User
mailing list