In Java, C# or C++, suppose we have a very common situation where we need to iterate a large amount of times and execute a function doX, but on only one of the iterations we should execute a function doY.
int index = 123456;
for(int i = 0; i < 1000000; i++)
{
if(i == index) doY();
else doX();
}
In situations where I see a real performance issue, I usually break the loop in 2, but this can be very painful, especially if the loop's body is large. Does the compiled code really check for the condition on every iteration, or can it be optimized by the compiler? Furthermore, if index
is not a constant at compile time, can there be such optimizations?