title: Profile C Execution
body: <p>So, just for fun, and out of curiosity, I wanted to see what executes faster when doing an even-odd check, modulus or bitwise comparisons.</p>
  
  <p>So, I whipped up the following, but I'm not sure that it's behaving correctly, as the difference is so small. I read somewhere online that bitwise should be an order of magnitude faster than modulus checking.</p>
  
  <p>Is it possible that it's getting optimized away? I've just started tinkering with assembly, otherwise I'd attempt to dissect the executable a bit.</p>
  
  <p><strong>EDIT 3:</strong> Here is a working test, thanks in a large way to @phonetagger:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdint.h&gt;
  
  // to reset the global
  static const int SEED = 0x2A;
  
  // 5B iterations, each
  static const int64_t LOOPS = 5000000000;
  
  int64_t globalVar;
  
  // gotta call something
  int64_t doSomething( int64_t input )
  {
    return 1 + input;
  }
  
  int main(int argc, char *argv[]) 
  {
    globalVar = SEED;
  
    // mod  
    clock_t startMod = clock();
  
    for( int64_t i=0; i&lt;LOOPS; ++i )
    {    
      if( ( i % globalVar ) == 0 )
      {
        globalVar = doSomething(globalVar);      
      }    
    }
  
    clock_t endMod = clock();
  
    double modTime = (double)(endMod - startMod) / CLOCKS_PER_SEC;
  
    globalVar = SEED;
  
    // bit
    clock_t startBit = clock();
  
    for( int64_t j=0; j&lt;LOOPS; ++j )
    {
      if( ( j &amp; globalVar ) == 0 )
      {
        globalVar = doSomething(globalVar);
      }       
    }
  
    clock_t endBit = clock();
  
    double bitTime = (double)(endBit - startBit) / CLOCKS_PER_SEC;
  
    printf("Mod: %lf\n", modTime);
    printf("Bit: %lf\n", bitTime);  
    printf("Dif: %lf\n", ( modTime &gt; bitTime ? modTime-bitTime : bitTime-modTime ));
  }
  </code></pre>
  
  <p>5 billion iterations of each loop, with a global removing compiler optimization yields the following:</p>
  
  <pre><code>Mod: 93.099101
  Bit: 16.701401
  Dif: 76.397700
  </code></pre>