Special cycle handling

In Java, C # or C ++, suppose we have a very common situation where we need to iterate over and execute the doX function many times, but only at one of the iterations do we have to execute the doY function.

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 split the cycle into 2, but it can be very painful, especially if the body of the cycle is large. Does the compiled code really check the condition at each iteration or can it be optimized by the compiler? Also, if indexit is not a constant at compile time, can there be such optimizations?

+4
source share
1

. . . .

- , , , if-statement. , . , . "" .

if false . .

, : " ?". , . , .

+10

Source: https://habr.com/ru/post/1627514/


All Articles