Cayden
Cayden

Reputation: 39

Speed considerations of if/else statements in for loops

Are there any differences in the general compute time in the following two scenarios? I remember learning in school that you should have the if statement run more often than the else statement (if possible), but wasn't really given any reasoning as to why this would be the case.

Lets say for both situations, condition is more likely than not to be true (for example, condition is true 75% and false 25% of the time).

Scenario 1:

for(int i=0; i<10000; ++i) {
    if(condition) {
        //do something
    } else {
        //do something else
    }
}

Scenario 2:

for(int i=0; i<10000; ++i) {
    if(!condition) {
        //do something else
    } else {
        //do something
    }
}

I guess the question boils down to, are there any speed differences between checking the if condition, running the do something code, and then branching to the end of the if/else statement compared to branching at the beginning and then running the do something code?

Edit:

Like many comments and answers have stated, there is likely no change in speed in these two different scenarios listed above. The situation I was remembering from school was in regards to nested if/else loops in which the following:

if(mostLikelyCondition) {
    if(lessLikelyCondtion) {
        //do something
    } else {
        //do something else
} else {
    //do other something else
}

would be faster than this:

if(lessLikelyCondition) {
    if(moreLikelyCondition) {
        //do something
    } else {
        //do something else
} else {
    //do other something else
}

Upvotes: 1

Views: 901

Answers (1)

Andreas Wenzel
Andreas Wenzel

Reputation: 24726

Modern optimizing compilers will be reorganizing your code so much that it probably won't matter which code is in the if block and which code is in the else block. This is especially true if profile-guided optimization is used.

Also, modern processors have very good branch prediction if the branch is inside a loop. Therefore, even if the branch is mispredicted in the first (few) loop iterations, it will likely adapt to the situation.

However, since these branch predictors are not perfect, it still can be important to minimize the number of branches. For the reasons illustrated in this answer to another StackOverflow question, there is a significant performance penality to the CPU if a branch is mispredicted.

Therefore, if you have a chain of if...else if statements, your code will probably run faster if the conditional expression in the first if statement has a high probability of being true, because in that case, the additional branches of the remaining else if statements will never be reached. If a branch is never reached, it can never be the cause of an expensive branch misprediction.

Also, if the conditional expression of the first if statement is true, then the remaining else if statements don't even have to be evaluated. This can also save performance, especially if the conditional expressions are expensive to evaluate.

Upvotes: 5

Related Questions