I'm profiling my code and optimized everything I could, coming down to a function which looks something like this:
double func(double a, double b, double c, double d, int i){
if(i > 10 && a > b || i < 11 && a < b)
return abs(a-b)/c;
else
return d/c;
}
It is called millions of times during the run of the program and profiler shows me that ~80% of all time is spent on calling abs()
.
I replaced
abs()
withfabs()
and it gave about 10% speed up which doesn't make much sense to me as I heard multiple times that they are identical for floating point numbers andabs()
should be used always. Is it untrue or I'm missing something?What would be the quickest way to evaluate absolute value for a double which could further improve the performance?
If that matters, I use g++
on linux X86_64.