title: Is a switch statement faster than a for loop?
body: <p>I am looking at the source code of the sparse bundle adjustment library <a href="http://users.ics.forth.gr/~lourakis/sba/index.html" rel="nofollow">(sba)</a> by Lourakis &amp; Argyros. More precisely I am looking at the following function <code>nrmL2xmy</code> which computes the squared L2 difference of two vectors. The following code is copied from the file <code>sba_levmar.c</code> starting from line <code>146</code>:</p>
  
  <pre><code>/* Compute e=x-y for two n-vectors x and y and return the squared L2 norm of e.
   * e can coincide with either x or y. 
   * Uses loop unrolling and blocking to reduce bookkeeping overhead &amp; pipeline
   * stalls and increase instruction-level parallelism; see http://www.abarnett.demon.co.uk/tutorial.html
   */
  static double nrmL2xmy(double *const e, const double *const x, const double *const y, const int n)
  {
  const int blocksize=8, bpwr=3; /* 8=2^3 */
  register int i;
  int j1, j2, j3, j4, j5, j6, j7;
  int blockn;
  register double sum0=0.0, sum1=0.0, sum2=0.0, sum3=0.0;
  
    /* n may not be divisible by blocksize, 
     * go as near as we can first, then tidy up.
     */
    blockn = (n&gt;&gt;bpwr)&lt;&lt;bpwr; /* (n / blocksize) * blocksize; */
  
    /* unroll the loop in blocks of `blocksize'; looping downwards gains some more speed */
    for(i=blockn-1; i&gt;0; i-=blocksize){
              e[i ]=x[i ]-y[i ]; sum0+=e[i ]*e[i ];
      j1=i-1; e[j1]=x[j1]-y[j1]; sum1+=e[j1]*e[j1];
      j2=i-2; e[j2]=x[j2]-y[j2]; sum2+=e[j2]*e[j2];
      j3=i-3; e[j3]=x[j3]-y[j3]; sum3+=e[j3]*e[j3];
      j4=i-4; e[j4]=x[j4]-y[j4]; sum0+=e[j4]*e[j4];
      j5=i-5; e[j5]=x[j5]-y[j5]; sum1+=e[j5]*e[j5];
      j6=i-6; e[j6]=x[j6]-y[j6]; sum2+=e[j6]*e[j6];
      j7=i-7; e[j7]=x[j7]-y[j7]; sum3+=e[j7]*e[j7];
    }
  
    /*
     * There may be some left to do.
     * This could be done as a simple for() loop, 
     * but a switch is faster (and more interesting) 
     */
  
    i=blockn;
    if(i&lt;n){ 
    /* Jump into the case at the place that will allow
     * us to finish off the appropriate number of items. 
     */
      switch(n - i){ 
        case 7 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
        case 6 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
        case 5 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
        case 4 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
        case 3 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
        case 2 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
        case 1 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
      }
    }
  
    return sum0+sum1+sum2+sum3;
  }
  </code></pre>
  
  <p>In the middle of the code (roughly), the authors state the following: </p>
  
  <pre><code> /*
     * There may be some left to do.
     * This could be done as a simple for() loop, 
     * but a switch is faster (and more interesting) 
     */
  </code></pre>
  
  <p>I don't understand why a <code>switch</code> is faster than a simple <code>for</code> loop. </p>
  
  <p>So my question is: <strong>Is this statement true?</strong> and if so, <strong>why</strong>? </p>