title: OrderedDict vs Dict in python
body: <p>In <a href="https://stackoverflow.com/a/18951209/1860929">Tim Peter's answer</a> to "Are there any reasons not to use an ordered dictionary", he says</p>
  
  <blockquote>
    <p>OrderedDict is a subclass of dict.</p>
    
    <p>It's not a lot slower, but at least doubles the memory over using a plain dict.</p>
  </blockquote>
  
  <p>Now, while going through a <a href="https://stackoverflow.com/q/25054003/1860929">particular question</a>, I tried some sample checks using <code>ipython</code> and both of them contradict the earlier reasoning:</p>
  
  <ol>
  <li>both <code>dict</code> and <code>OrderedDict</code> are of same size</li>
  <li>operating on an <code>OrderedDict</code> takes easily around 7-8 times more time than operating on a <code>dict</code> (Hence a lot slower)</li>
  </ol>
  
  <p>Can someone explain to me where I'm going wrong in my reasoning?</p>
  
  <hr>
  
  <h3>Create a large Dict and OrderedDict and compare sizes:</h3>
  
  <pre><code>import sys
  import random
  from collections import OrderedDict
  
  test_dict = {}
  test_ordered_dict = OrderedDict()
  
  for key in range(10000):
      test_dict[key] = random.random()
      test_ordered_dict[key] = random.random()
  
  sys.getsizeof(test_dict)
  786712
  
  sys.getsizeof(test_ordered_dict)
  786712
  </code></pre>
  
  <h3>Check time taken for the insertions using <code>%timeit</code>:</h3>
  
  <pre><code>import sys
  import random
  from collections import OrderedDict
  
  def operate_on_dict(r):
      test_dict = {}
      for key in range(r):
          test_dict[key] = random.random()
  
  def operate_on_ordered_dict(r):
      test_ordered_dict = OrderedDict()
      for key in range(r):
          test_ordered_dict[key] = random.random()
  
  %timeit for x in range(100): operate_on_ordered_dict(100)
  100 loops, best of 3: 9.24 ms per loop
  
  %timeit for x in range(100): operate_on_dict(100)
  1000 loops, best of 3: 1.23 ms per loop
  </code></pre>