title: Lookup table vs switch in C embedded software
body: <p>In another thread, I was told that a <code>switch</code> may be better than a <em>lookup table</em> in terms of speed and compactness.</p>
  
  <p>So I'd like to understand the differences between this:</p>
  
  <h2>Lookup table</h2>
  
  <pre><code>static void func1(){}
  static void func2(){}
  
  typedef enum
  {
      FUNC1,
      FUNC2,
      FUNC_COUNT
  } state_e;
  
  typedef void (*func_t)(void);
  
  const func_t lookUpTable[FUNC_COUNT] =
  {
      [FUNC1] = &amp;func1,
      [FUNC2] = &amp;func2
  };
  
  void fsm(state_e state)
  {
      if (state &lt; FUNC_COUNT) 
          lookUpTable[state]();
      else
          ;// Error handling
  }
  </code></pre>
  
  <p>and this:</p>
  
  <h2>Switch</h2>
  
  <pre><code>static void func1(){}
  static void func2(){}
  
  void fsm(int state)
  {
      switch(state)
      {
          case FUNC1: func1(); break;
          case FUNC2: func2(); break;
          default:    ;// Error handling
      }
  }
  </code></pre>
  
  <p>I thought that a lookup table was faster since compilers try to transform switch statements into jump tables when possible.
  Since this may be wrong, I'd like to know why!</p>
  
  <p>Thanks for your help!</p>