There are tons of questions and good answers concerning compiler optimization about "redundant function calls" on SO (I won't post links), however, I could not find anything on multiple same function calls on SO.
Say I have a code snippet like this:
void fairlyComplexFunction(const double &angle)
{
//do stuff and call "sin(angle)" very often in here
}
Calling sin(angle) is a fairly expensive operation and since angle is a const within the scope of fairlyComplexFunction every call of the sine will end up with the same result so only calling it once would be a better approach:
void fairlyComplexFunction(const double &angle)
{
const double sineOfAngle = sin(angle);
//do stuff and use sineOfAngle very often in here
}
Is a compiler in any way able to detect things like these and optimize this for me or is the second example a better approach?
double S= sin(Angle)can save you a lot of typing...