Fanning Software Consulting

Faster Exponent Operations

QUESTION: 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

When I run this code, IDL prints out:

      0.024163008
     0.010262012 

Apparently multiplying an array by itself is twice as fast as using an exponent operator! Do know why this is? Is it a memory issue or something?

ANSWER: The answer was supplied by Bruce Bowler and Paolo Grigis. Here is Bruce Bowler's reply.

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 (using an integer as the exponent) and see how the results change.

Sure enough, the results changed. Here are the same numbers now.

     0.018048048      0.010533094 

It is unlikely that this kind of speed difference is making a big difference in how fast your code runs (get rid of your FOR loops first), but it is something to know.

Version of IDL used to prepare this article: IDL 7.0.3.

Google
 
Web Coyote's Guide to IDL Programming