Hi everybody,
I have a simple mathematical member function of a class:
struct shell
{
double mc, ms, r0, v0, a0, w;
shell(double mc, double ms, double r0, double v0, double w);
double absv(double r) const;
};
shell::shell(double mc, double ms, double r0, double v0, double w)
{
this->mc = mc;
this->ms = ms;
this->r0 = r0;
this->v0 = v0;
a0 = v0*v0 - (2*mc+ms)/r0;
this->w = w;
}
double shell::absv(double r) const
{
return sqrt((2*mc+ms)/r + a0 + 4*w*log(r/r0));
}
The function absv is called lots of times during my run,
therefore I decide to try to speed up if it is possible.
But my experiences are very strange and unexpected for me!
First:
I introduce b0 = 2*mc+ms as a member variable,
and exchange absv to sqrt(b0/r + a0 + 4*w*log(r/r0));
But it is now slower! Why?
Second:
I add the following line to absv:
if (w == 0) return sqrt((2*mc+ms)/r + a0);
And its much more rapid for w=0, but why?
I expect that the * operator doesnt try to calculate log() if w=0,
becaase 0*anything = 0