title: Delphi Performance: Case Versus If
body: <p>I guess there might be some overlapping with previous SO questions, but I could not find a Delphi-specific question on this topic.</p>
  
  <p>Suppose that you want to check if an unsigned 32-bit integer variable "MyAction" is equal to any of the constants ACTION1, ACTION2, ..., ACTIONn, where n is - say 1000. I guess that, besides being more elegant,</p>
  
  <pre><code>case MyAction of
    ACTION1: {code};
    ACTION2: {code};
    ...
    ACTIONn: {code};
  end;
  </code></pre>
  
  <p>is much faster than</p>
  
  <pre><code>if MyAction = ACTION1 then
    // code
  else if MyAction = ACTION2 then
    // code
  ...
  else if MyAction = ACTIONn then
    // code;
  </code></pre>
  
  <p>I guess that the if variant takes time O(n) to complete (i.e. to find the right action) if the right action ACTIONi has a high value of i, whereas the case variant takes a lot less time (O(1)?).</p>
  
  <ol>
  <li>Am I correct that switch is much faster?</li>
  <li>Am I correct that the time required to find the right action in the switch case actually is independent of n? I.e. is it true that it does not really take any longer to check a million cases than to check 10 cases?</li>
  <li>How, exactly, does this work?</li>
  </ol>