comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: x*x versus x^2
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: x*x versus x^2 [message #61199] Wed, 09 July 2008 10:10 Go to next message
Conor is currently offline  Conor
Messages: 138
Registered: February 2007
Senior Member
On Jul 9, 12:57 pm, Bruce Bowler <bbow...@bigelow.org> wrote:
> On Wed, 09 Jul 2008 09:43:27 -0700, Conor wrote:
>> On Jul 9, 12:32 pm, Conor <cmanc...@gmail.com> wrote:
>>> So I've been looking at execution time for various algorithms, and I
>>> found this interesting result:
>
>>> bigarr = fltarr(1000,1000)
>
>>> t1 = systime(/seconds)
>>> t = bigarr^2.0
>>> t2 = systime(/seconds)
>>> t = bigarr*bigarr
>>> t3 = systime(/seconds)
>
>>> print,t2-t1
>>> print,t3-t2
>
>>> IDL prints:
>
>>> 0.024163008
>>> 0.010262012
>
>>> Apparently multiplying an array by itself is twice as fast as using the
>>> carat operator! Anyone know why this is? Is it a memory issue or
>>> something?
>
>> This also holds true for array's smaller than the multi-threading
>> minimum size, so it isn't because multi-threading is being used in one
>> case but not the other...
>
> Digging into the deep dark recesses of my brain...
>
> exponentiation with a real exponent generally uses the log function to do
> it's thing. *some* language implementations are smart enough that if the
> exponent is an integer, they decompose the exponentiation into
> multiplication.
>
> It might be worth trying your experiment with t=bigarr^2 and see how the
> results change.
>
> Bruce

Interesting... I tried your suggestion and got this result:

0.018048048
0.010533094

So it is still slower, but the difference is smaller. A calculation
like this is rarely the bottleneck for speed in a program, so I
probably won't worry about it too much, but it is an interesting fact
to be aware of...
Re: x*x versus x^2 [message #61200 is a reply to message #61199] Wed, 09 July 2008 09:57 Go to previous messageGo to next message
Bruce Bowler is currently offline  Bruce Bowler
Messages: 128
Registered: September 1998
Senior Member
On Wed, 09 Jul 2008 09:43:27 -0700, Conor wrote:

> On Jul 9, 12:32 pm, Conor <cmanc...@gmail.com> wrote:
>> So I've been looking at execution time for various algorithms, and I
>> found this interesting result:
>>
>> bigarr = fltarr(1000,1000)
>>
>> t1 = systime(/seconds)
>> t = bigarr^2.0
>> t2 = systime(/seconds)
>> t = bigarr*bigarr
>> t3 = systime(/seconds)
>>
>> print,t2-t1
>> print,t3-t2
>>
>> IDL prints:
>>
>> 0.024163008
>> 0.010262012
>>
>> Apparently multiplying an array by itself is twice as fast as using the
>> carat operator! Anyone know why this is? Is it a memory issue or
>> something?
>
> This also holds true for array's smaller than the multi-threading
> minimum size, so it isn't because multi-threading is being used in one
> case but not the other...

Digging into the deep dark recesses of my brain...

exponentiation with a real exponent generally uses the log function to do
it's thing. *some* language implementations are smart enough that if the
exponent is an integer, they decompose the exponentiation into
multiplication.

It might be worth trying your experiment with t=bigarr^2 and see how the
results change.

Bruce
Re: x*x versus x^2 [message #61201 is a reply to message #61200] Wed, 09 July 2008 09:55 Go to previous messageGo to next message
pgrigis is currently offline  pgrigis
Messages: 436
Registered: September 2007
Senior Member
Probably this happens because you raise to a floating point
exponent, and likely IDL computes an exponential function,
which is slower.
If you try x^2 (not x^2.0) it is possible that the compiler
optimizes it to x*x.

Ciao,
Paolo

Conor wrote:
> So I've been looking at execution time for various algorithms, and I
> found this interesting result:
>
> bigarr = fltarr(1000,1000)
>
> t1 = systime(/seconds)
> t = bigarr^2.0
> t2 = systime(/seconds)
> t = bigarr*bigarr
> t3 = systime(/seconds)
>
> print,t2-t1
> print,t3-t2
>
>
> IDL prints:
>
> 0.024163008
> 0.010262012
>
> Apparently multiplying an array by itself is twice as fast as using
> the carat operator! Anyone know why this is? Is it a memory issue or
> something?
Re: x*x versus x^2 [message #61203 is a reply to message #61201] Wed, 09 July 2008 09:43 Go to previous messageGo to next message
Conor is currently offline  Conor
Messages: 138
Registered: February 2007
Senior Member
On Jul 9, 12:32 pm, Conor <cmanc...@gmail.com> wrote:
> So I've been looking at execution time for various algorithms, and I
> found this interesting result:
>
> bigarr = fltarr(1000,1000)
>
> t1 = systime(/seconds)
> t = bigarr^2.0
> t2 = systime(/seconds)
> t = bigarr*bigarr
> t3 = systime(/seconds)
>
> print,t2-t1
> print,t3-t2
>
> IDL prints:
>
> 0.024163008
> 0.010262012
>
> Apparently multiplying an array by itself is twice as fast as using
> the carat operator! Anyone know why this is? Is it a memory issue or
> something?

This also holds true for array's smaller than the multi-threading
minimum size, so it isn't because multi-threading is being used in one
case but not the other...
Re: x*x versus x^2 [message #61287 is a reply to message #61199] Sat, 12 July 2008 09:25 Go to previous message
dzhang49 is currently offline  dzhang49
Messages: 1
Registered: July 2008
Junior Member
On Jul 9, 11:10 am, Conor <cmanc...@gmail.com> wrote:
> On Jul 9, 12:57 pm, Bruce Bowler <bbow...@bigelow.org> wrote:
>
>
>
>> On Wed, 09 Jul 2008 09:43:27 -0700, Conor wrote:
>>> On Jul 9, 12:32 pm, Conor <cmanc...@gmail.com> wrote:
>>>> So I've been looking at execution time for various algorithms, and I
>>>> found this interesting result:
>
>>>> bigarr = fltarr(1000,1000)
>
>>>> t1 = systime(/seconds)
>>>> t = bigarr^2.0
>>>> t2 = systime(/seconds)
>>>> t = bigarr*bigarr
>>>> t3 = systime(/seconds)
>
>>>> print,t2-t1
>>>> print,t3-t2
>
>>>> IDL prints:
>
>>>> 0.024163008
>>>> 0.010262012
>
>>>> Apparently multiplying an array by itself is twice as fast as using the
>>>> carat operator! Anyone know why this is? Is it a memory issue or
>>>> something?
>
>>> This also holds true for array's smaller than the multi-threading
>>> minimum size, so it isn't because multi-threading is being used in one
>>> case but not the other...
>
>> Digging into the deep dark recesses of my brain...
>
>> exponentiation with a real exponent generally uses the log function to do
>> it's thing. *some* language implementations are smart enough that if the
>> exponent is an integer, they decompose the exponentiation into
>> multiplication.
>
>> It might be worth trying your experiment with t=bigarr^2 and see how the
>> results change.
>
>> Bruce
>
> Interesting... I tried your suggestion and got this result:
>
> 0.018048048
> 0.010533094
>
> So it is still slower, but the difference is smaller. A calculation
> like this is rarely the bottleneck for speed in a program, so I
> probably won't worry about it too much, but it is an interesting fact
> to be aware of...

Actually, if you increase the dimension of the array, the result will
be reverse, here it is:
pro test_speed
bigarr = fltarr(10000,10000)
t1 = systime(/seconds)
t = bigarr^2.0
t2 = systime(/seconds)
t = bigarr*bigarr
t3 = systime(/seconds)

print,t2-t1
print,t3-t2
end

and the results are :
0.68420601
0.83076620

So if you run a larger number, the ^2 will be faster
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: newbie wants to call IDL from shell
Next Topic: Re: IDL Adventure!?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 10:10:28 PDT 2025

Total time taken to generate the page: 0.47913 seconds