title: Small lookup table vs if-else and performance
body: <p>I have few billon of record and their first character is used to identify an action to be taken. I have been asked to remove if else from code and to check performance. So I created a small lookup table with <code>char</code> as an array index and stored function pointers at those indices. Code is as following:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int add_check, mod_check, del_check, strike_check;
  void add(int rnum){
    add_check = add_check + rnum;
  }
  void modify(int rnum){
    mod_check = mod_check + rnum;
  }
  void delete(int rnum){
    del_check = del_check + rnum;
  }
  void strike (int rnum) {
    strike_check = strike_check + rnum;
  }
  
  int main(void){
    void (*action_fn[91])(int);
    memset (action_fn, 0, sizeof(action_fn));
    add_check = mod_check = del_check = strike_check = 0;
    action_fn['A'] = add;
    action_fn['M'] = modify;
    action_fn['D'] = delete;
    action_fn['S'] = strike;
  
    unsigned char msg[] = {
      'A', 'D', 'M', 'D', 'M', 'S','A', 'D', 'M','S', 'A', 'A', 'A', 'D','D',
      'D', 'M','A','D', 'A', 'A', 'A','M','M','S','M','S','M', 'S','A','A',
      'D', 'M','M','M','S', 'A','D','M', 'A','S','M','D','D','D','D','A',
      'A','A','A', 'A', 'D', 'M', 'D', 'M', 'A', 'S','S','S','D', 'M','S' ,
      'A', 'A', 'A', 'D','D','D', 'A','D','M', 'A','M','D','D','D','D','A',
      'A','A','A', 'M','A','D', 'A', 'A', 'A','M','M','M','M', 'A','A','D',
      'M','M','M', 'A','M','D','A','A','A','A','M','M','D','D'
    };
  
    clock_t start;
    double elapsed_time;
  
    start = clock();
    for (int i = 0; i &lt; (int)sizeof(msg); i++ ){
      if (msg[i] == 'A'){
        add(1); 
      } else if (msg[i] == 'M') {
        modify(1);
      } else if (msg[i] == 'D'){
        delete(1);
      } else if (msg[i] == 'S'){
        strike(1);
      }
    }
    elapsed_time = (double)(clock() - start) / CLOCKS_PER_SEC;
    printf(&quot;ElapsedTime-ifelse:%0.6f\n&quot;, elapsed_time);
    printf(&quot;add_check:%d mod_check:%d del_check:%d strike_check:%d\n&quot;,
        add_check, mod_check, del_check, strike_check);
    
    start = clock();
    for (int i = 0; i &lt; (int)sizeof(msg); i++ ){
      action_fn[msg[i]](1);
    }
    elapsed_time = (double)(clock() - start) / CLOCKS_PER_SEC;
    printf (&quot;ElapsedTime-fp:%0.7f\n&quot;, elapsed_time);
    printf (&quot;add_check:%d mod_check:%d del_check:%d&quot;
          &quot;strike_check :%d\n&quot;,
        add_check, mod_check, del_check, strike_check);
    
    return 0;
  }
  
  
  </code></pre>
  <p>So I have following doubt since this simple test is not giving any clear results. Will this lookup table help in improving performance when number of records are really high and Add,Mod,Del and Strike is random? Or it will not help at all since there are limited conditions or lookup table will perform badly compared to if-else?</p>