title: Performance of array of functions over if and switch statements
body: <p>I am writing a very performance critical part of the code and I had this crazy idea about substituting case statements (or if statements) with array of function pointers.</p>
  
  <p>Let me demonstrate; here goes the normal version:</p>
  
  <pre><code>while(statement)
  {
      /* 'option' changes on every iteration */
  
      switch(option)
      {
          case 0: /* simple task */ break;
          case 1: /* simple task */ break;
          case 2: /* simple task */ break;
          case 3: /* simple task */ break;
      }
  }
  </code></pre>
  
  <p>And here is the "callback function" version:</p>
  
  <pre><code>void task0(void) {
      /* simple task */
  }
  
  void task1(void) {
      /* simple task */
  }
  
  void task2(void) {
      /* simple task */
  }
  
  void task3(void) {
      /* simple task */
  }
  
  void (*task[4]) (void);
  
  task[0] = task0;
  task[1] = task1;
  task[2] = task2;
  task[3] = task3;
  
  while(statement)
  {
      /* 'option' changes on every iteration */
  
      /* and now we call the function with 'case' number */
      (*task[option]) ();
  }
  </code></pre>
  
  <p>So which version will be faster? Is the overhead of the function call eliminating speed benefit over normal switch (or if) statement?</p>
  
  <p>Ofcourse the latter version is not so readable but I am looking for all the speed I can get.</p>
  
  <p>I am about to benchmark this when I get things set up but if someone has an answer already, I wont bother.</p>