title: Comparison operator performance <= against !=
body: <p>Lets start out with stating code-readability beats micro-optimizations and we should rather leave that to the compiler. This was just a weird case where the specifics seemed interesting against general recommendation</p>
  
  <p>So was messing about with a Prime number generator function, and came up with a weird behavior where "!=" which people recommend to be the most efficient actually the least efficient and "&lt;=" which is the worst as the best option.</p>
  
  <p><strong>C#</strong></p>
  
  <pre><code>private static void Main(string[] args) {
    long totalTicks = 0;
    for (int i = 0; i &lt; 100; ++i) {
      var stopWatch = Stopwatch.StartNew();
      PrintPrimes(15000);
      totalTicks += stopWatch.ElapsedTicks;
    }
    Console.WriteLine("\n\n\n\nTick Average: {0}", totalTicks / 100);
    Console.Read();
  }
  
  private static void PrintPrimes(int numberRequired) {
    if (numberRequired &lt; 1)
      return;
    Console.Write("{0}\t", 2);
    int primeTest = 3;
    /****** UPDATE NEXT TWO LINES TO TEST FOR != *****/
    int numPrimes = 2;  // set numPrimes = 1 for !=
    while (numPrimes &lt;= numberRequired) {  // switch &lt;= to !=
      if (IsPrime(primeTest)) {
        Console.Write("{0}\t", primeTest);
        ++numPrimes;
      }
      primeTest += 2;
    }
  }
  
  private static bool IsPrime(int test) {
    for (int i = 3; i * i &lt;= test; i = 2 + i)
      if (test % i == 0)
        return false;
    return true;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;= 1319991
  != 1321251
  </code></pre>
  
  <p><strong>Similarly In C++(On a different machine)</strong></p>
  
  <pre><code>include &lt;cstddef&gt;
  #include &lt;limits&gt;
  
  int main() {
    for(size_t i(0) ; i &lt;= 10000000000 ; ++i);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;=
  
  real        0m16.538s
  user        0m16.460s
  sys        0m0.000s
  ~ [master] $ vim d.cc
  
  !=
  
  real        0m16.860s
  user        0m16.780s
  sys        0m0.000s
  </code></pre>
  
  <p>The loops run the same amount of times. Are there any optimizations for <code>&lt;=</code> which does not apply for <code>!=</code> or is it some weird cpu behavior?</p>