I'm trying to optimize the following piece of code, which is a bottleneck in my application. What it does: It takes the double values value1 and value2 and tries to find the maximum including a correctional factor. If the difference between both values is greater than 5.0 (the LUT is scaled by factor 10), I can just take the max value of those two. If the difference is smaller than 5.0, I can use the correctional factor from the LUT.
Does anyone have an idea what could be a better style for this piece of code? I don't know where I'm losing time - is it the large number of ifs or the multiplication by 10?
double value1, value2;
// Lookup Table scaled by 10 for (ln(1+exp(-abs(x)))), which is almost 0 for x > 5 and symmetrical around 0. LUT[0] is x=0.0, LUT[40] is x=4.0.
const logValue LUT[50] = { ... }
if (value1 > value2)
{
if (value1 - value2 >= 5.0)
{
return value1;
}
else
{
return value1 + LUT[(uint8)((value1 - value2) * 10)];
}
}
else
{
if (value2 - value1 >= 5.0)
{
return value2;
}
else
{
return value2 + LUT[(uint8)((value2 - value1) * 10)];
}
}