EDIT: after removing UB (good place, I missed it), the times are more or less identical. A moderator will be marked to remove it.
These two functions are identical, except for the fact that it foohas a return inside if, on both branches, while it goohas one returnat the end:
int foo()
{
static int x = 0;
if ( x )
{
x > 2 ? x = 0 : ++x;
return x-1;
}
else
{
x++;
return x-1;
}
}
int goo()
{
static int x = 0;
if ( x )
{
x > 2 ? x = 0 : ++x;
}
else
{
x++;
}
return x-1;
}
The numbers are here, so the optimization is not hit too hard, and the function call is not optimized. Compiled with full optimization on MSVS 2010.
Function call 4,000,000,000 times, fetch 10 times, alwaysfoo faster :
foo- 8830 ms averagegoo- 8703 ms average
, . ? , ?