title: Speed difference between If-Else and Ternary operator in C...?
body: <p>So at the suggestion of a colleague, I just tested the speed difference between the ternary operator and the equivalent If-Else block... and it seems that the ternary operator yields code that is between 1x and 2x faster than If-Else. My code is:</p>
  
  <pre><code>  gettimeofday(&amp;tv3, 0);
    for(i = 0; i &lt; N; i++)
    {
       a = i &amp; 1;
       if(a) a = b; else a = c;
    }
    gettimeofday(&amp;tv4, 0);
  
  
    gettimeofday(&amp;tv1, 0);
    for(i = 0; i &lt; N; i++)
    {
       a = i &amp; 1;
       a = a ? b : c;
    }
    gettimeofday(&amp;tv2, 0);
  </code></pre>
  
  <p>(Sorry for using gettimeofday and not clock_gettime... I will endeavor to better myself.)</p>
  
  <p>I tried changing the order in which I timed the blocks, but the results seem to persist. What gives? Also, the If-Else shows much more variability in terms of execution speed. Should I be examining the assembly that gcc generates?</p>
  
  <p><strong>By the way, this is all at optimization level zero (-O0).</strong></p>
  
  <p>Am I imagining this, or is there something I'm not taking into account, or is this a machine-dependent thing, or what? Any help is appreciated.</p>