title: C++ massive performance loss because of if statement
body: <p>I am running while loop in 4 thread, in the loop I am evaluating function and incrementally increasing counter. </p>
  
  <pre><code>while(1) {
      int fitness = EnergyFunction::evaluate(sequence);
  
      mutex.lock();
      counter++;
      mutex.unlock();
  }
  </code></pre>
  
  <p>When I run this loop, as I said in 4 running threads, I get ~ 20 000 000 evaluations per second.</p>
  
  <pre><code>while(1) {
      if (dist(mt) == 0) {
          sequence[distDim(mt)] = -1;
      } else {
          sequence[distDim(mt)] = 1;
      }
      int fitness = EnergyFunction::evaluate(sequence);
  
      mainMTX.lock();
      overallGeneration++;
      mainMTX.unlock();
  }
  </code></pre>
  
  <p>If I add some random mutation for the sequence, I get ~ 13 000 000 evaluations per second.</p>
  
  <pre><code>while(1) {
      if (dist(mt) == 0) {
          sequence[distDim(mt)] = -1;
      } else {
          sequence[distDim(mt)] = 1;
      }
      int fitness = EnergyFunction::evaluate(sequence);
  
      mainMTX.lock();
      if(fitness &lt; overallFitness)
          overallFitness = fitness;
  
      overallGeneration++;
      mainMTX.unlock();
  }
  </code></pre>
  
  <p>But when I add simple if statement that checks, if new fitness is smaller than old fitness if that is true then replace old fitness with new fitness.</p>
  
  <p>But performance loss is massive! Now I get ~ 20 000 evaluations per second. If I remove random mutation part, I also get ~ 20 000 evaluations per second.</p>
  
  <p>Variable overallFitness is declared as</p>
  
  <pre><code>extern int overallFitness; 
  </code></pre>
  
  <p>I am having troubles figuring out what is the problem for such a big performance loss. Is comparing two int such time taking operation? </p>
  
  <p>Also I don't believe that is related to mutex locking.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This performance loss was not because of branch prediction, but compiler just ignored this call <code>int fitness = EnergyFunction::evaluate(sequence);</code>.</p>
  
  <p>Now I added <code>volatile</code> and compiler doesn't ignore the call anymore.</p>
  
  <p>Also thank you for pointing out branch misprediction and <code>atomic&lt;int&gt;</code>, didn't know about them!</p>
  
  <p>Because of atomic I also remove mutex part, so the final code looks like this:</p>
  
  <pre><code>while(1) {
      sequence[distDim(mt)] = lookup_Table[dist(mt)];
      fitness = EnergyFunction::evaluate(sequence);
      if(fitness &lt; overallFitness)
         overallFitness = fitness;
      ++overallGeneration;
  }
  </code></pre>
  
  <p>Now I am getting ~ 25 000 evaluations per second. </p>