title: Why is dict faster than if-else in python?
body: <p>I tried to compare dict and if-else which is faster as follows.</p>
  
  <pre><code>d = {
      str  : lambda x: "'%s'" % str(x),
      int  : lambda x: str(x),
      float: lambda x: str(x),
  }
  items = ['a', 'b', 'c', 1, 2, 3, 4, 5, 1.0]
  
  def use_dict():
      r = []
      for i in items:
          r.append(d[type(i)](i))
      return r
  
  def use_if():
      r = []
      for i in items:
          if isinstance(i, str):
              r.append("'%s'" % str(i))
          elif isinstance(i, (int, float)):
              r.append(str(i))
      return r
  
  if __name__ == '__main__':
  
      from timeit import timeit
  
      print 'use_dict:', timeit(use_dict)
      # -&gt; use_dict: 9.21109666657
  
      print 'use_if  :', timeit(use_if)
      # -&gt; use_if  : 10.9568739652
  </code></pre>
  
  <p>I found dict is faster than if-else. This means when I want to write a switch-statement, dict is better solution. But I have a doubt Why dict is faster? Anyone can explain it. Thanks.</p>