title: Performance of branch prediction in a loop
body: <p>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.</p>
  
  <p><em>Snippet 1:</em></p>
  
  <pre><code>for (int i = 0; i &lt; 100; i++) {
      if (a == 3)
          output[i] = 1;
      else
          output[i] = 0;
  }
  </code></pre>
  
  <p><em>Snippet 2:</em></p>
  
  <pre><code>if (a == 3) {
      for (int i = 0; i &lt; 100; i++)
          output[i] = 1;
  } else {
      for (int i = 0; i &lt; 100; i++)
          output[i] = 0;
  }
  </code></pre>
  
  <p>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.</p>