- Joined
- Apr 3, 2002
- Location
- Bloomington, IN
I was messing with some C++ code today using the standard gcc/g++ compiler.
I'm not sure about you all, but I use the function "pow( double, double)" all the time. Particularly useful for scientific computing, I suppose, but also for graphics programming. In particular, I use things like
Radius = pow( x, 2.0) + pow( y , 2.0);
quite a bit.
I found that code I compiled ran very slowly. In particular, one algorithm I have for reinitializing a 150x150 data set in a particular way would take 5-10 minutes on a P4 running with fully optimized code. (-O3, -march=pentium4, etc.)
Well, the other day, I was writing some scratch code, and for the heck of it, I tried things like x*x instead of pow(x,2.0), and the resulting code was ten times faster. I couldn't believe it. Well, today I went back and made a new function:
and used that anytime I had integer powers. As expected, it sped the costliest function of my computational tumor growth function up from taking 5-10 minutes to 4-5 seconds. It was just amazing.
I'd assume this is because pow(double,double) needs to be a lot more complicated to support things like pow( 3.5, 1.5 ). It leaves me very tempted to compare the performance of sqrt(x) and pow(x,0.5). Fascinating stuff.
Anyway, since exponents are used so frequently in coding, I thought I'd share that with all of you. It's amazing what a difference such a small change can make!
-- Paul
I'm not sure about you all, but I use the function "pow( double, double)" all the time. Particularly useful for scientific computing, I suppose, but also for graphics programming. In particular, I use things like
Radius = pow( x, 2.0) + pow( y , 2.0);
quite a bit.
I found that code I compiled ran very slowly. In particular, one algorithm I have for reinitializing a 150x150 data set in a particular way would take 5-10 minutes on a P4 running with fully optimized code. (-O3, -march=pentium4, etc.)
Well, the other day, I was writing some scratch code, and for the heck of it, I tried things like x*x instead of pow(x,2.0), and the resulting code was ten times faster. I couldn't believe it. Well, today I went back and made a new function:
Code:
double intpow( double base, int exponent )
{
int i;
double out = base;
for( i=1 ; i < exponent ; i++ )
{
out *= base;
}
return out;
}
and used that anytime I had integer powers. As expected, it sped the costliest function of my computational tumor growth function up from taking 5-10 minutes to 4-5 seconds. It was just amazing.
I'd assume this is because pow(double,double) needs to be a lot more complicated to support things like pow( 3.5, 1.5 ). It leaves me very tempted to compare the performance of sqrt(x) and pow(x,0.5). Fascinating stuff.
Anyway, since exponents are used so frequently in coding, I thought I'd share that with all of you. It's amazing what a difference such a small change can make!
-- Paul