Search code examples
c++performanceoptimizationbranch-prediction

Performance of branch prediction in a loop


Would there be any noticeable speed difference between these two snippets of code? Naively, I think the second snippet would be faster because branch instructions are encountered a lot less, but on the other hand the branch predictor should solve this problem. Or will it have a noticeable overhead despite the predictable pattern? Assume that no conditional move instruction is used.

Snippet 1:

for (int i = 0; i < 100; i++) {
    if (a == 3)
        output[i] = 1;
    else
        output[i] = 0;
}

Snippet 2:

if (a == 3) {
    for (int i = 0; i < 100; i++)
        output[i] = 1;
} else {
    for (int i = 0; i < 100; i++)
        output[i] = 0;
}

I'm not intending to optimise these cases myself, but I would like to know more about the overhead of branches even with a predictable pattern.


Solution

  • Since a remains unchanged once you enter into the loop, there shouldn't be much difference between the two code-snippet.

    Personally, I would prefer the former, unless branch predictor fails to predict the branch which is really unlikely, given that a remains unchanged in the loop.

    Moreover, the compiler may perform this optimization:

    thereby making both code-snippets emit exactly same machine instructions.