MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi
Learn moreOpportunities for recent engineering grads.
Apply TodayMATLAB Central > MATLAB Newsreader > Powers are slow, multiplies fast; optimized badly? 

Subject: Powers are slow, multiplies fast; optimized badly? From: Irl Date: 19 Jul, 2010 17:49:04 Message: 1 of 11 
MATLAB seems to implement x^2 as a multiply but x^3, or higher power, via something much slower (exp of number times log?). For large enough n, exp n log will be faster than multiplying n times, but the internal code generator seems to err wildly on the side of using the slow method, whatever it is, rather than just repeated multiplies. But it does do x^2, apparently, as x*x. Is there a way to make it use repeated multiplies for higher exponents? I wrote a Zernike polynomial program and it's very tiresome to go change all the powers to multiply strings. 
Subject: Powers are slow, multiplies fast; optimized badly? From: Walter Roberson Date: 19 Jul, 2010 18:25:46 Message: 2 of 11 
Irl wrote: 
Subject: Powers are slow, multiplies fast; optimized badly? From: James Tursa Date: 19 Jul, 2010 20:24:04 Message: 3 of 11 
Walter Roberson <roberson@hushmail.com> wrote in message <i225i6$anh$1@canopus.cc.umanitoba.ca>... 
Subject: Powers are slow, multiplies fast; optimized badly? From: Bruno Luong Date: 19 Jul, 2010 21:56:04 Message: 4 of 11 
"Irl " <irl_smith@raytheonRemoveTheseWords.com> wrote in message <i2236g$oco$1@fred.mathworks.com>... 
Subject: Powers are slow, multiplies fast; optimized badly? From: Steve Amphlett Date: 20 Jul, 2010 06:46:05 Message: 5 of 11 
"Irl " <irl_smith@raytheonRemoveTheseWords.com> wrote in message <i2236g$oco$1@fred.mathworks.com>... 
Subject: Powers are slow, multiplies fast; optimized badly? From: Bruno Luong Date: 20 Jul, 2010 06:49:04 Message: 6 of 11 
I dig out the old thread where the binary decomposition power algorithm is provided (need to change '*' to '.*' to adapt to the context): 
Subject: Powers are slow, multiplies fast; optimized badly? From: Bruno Luong Date: 20 Jul, 2010 11:10:05 Message: 7 of 11 
Here is a benchmark with the binary method for integer power (Matlab 2010A 64, Vista): 
Subject: Powers are slow, multiplies fast; optimized badly? From: dpb Date: 20 Jul, 2010 12:38:12 Message: 8 of 11 
Steve Amphlett wrote: 
Subject: Powers are slow, multiplies fast; optimized badly? From: Bruno Luong Date: 20 Jul, 2010 14:35:05 Message: 9 of 11 
This is a more compact version of the function allpow() in post #4 
Subject: Powers are slow, multiplies fast; optimized badly? From: Irl Date: 20 Jul, 2010 15:13:06 Message: 10 of 11 
Dear Bruno, Steve, Walter, et al.  
Subject: Powers are slow, multiplies fast; optimized badly? From: Dan Scholnik Date: 31 Dec, 2012 01:15:10 Message: 11 of 11 
An update to this thread: I found that even intpow() above is not as well optimized as is a direct commandline call to repeated multiplies, even when grouped identically to those done in intpow. In the script output below, I compare using .^, intpow, a series of ungrouped multiplies, and a series of multiplies grouped to match intpow. You can see that grouping a series of multiplications does affect the computation, but does not seem to affect the time of the computation, and repeated multiplies are much faster than intpow even though it seems to be doing the same or even fewer multiplies. I've also shown peak and rms errors between the various methods, to give some idea of how the errors go with the exponent. From this sample it seems that ungrouped multiplies is slightly more accurate with respect to the .^ operator than is intpow(). 
A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.
Anyone can tag a thread. Tags are public and visible to everyone.