title: Dictionary vs Object - which is more efficient and why?
body: <p>What is more efficient in Python in terms of memory usage and CPU consumption - Dictionary or Object?</p>
  
  <p><strong>Background:</strong>
  I have to load huge amount of data into Python. I created an object that is just a field container. Creating 4M instances and putting them into a dictionary took about 10 minutes and ~6GB of memory. After dictionary is ready, accessing it is a blink of an eye.</p>
  
  <p><strong>Example:</strong>
  To check the performance I wrote two simple programs that do the same - one is using objects, other dictionary:</p>
  
  <p>Object (execution time ~18sec):</p>
  
  <pre><code>class Obj(object):
    def __init__(self, i):
      self.i = i
      self.l = []
  all = {}
  for i in range(1000000):
    all[i] = Obj(i)
  </code></pre>
  
  <p>Dictionary (execution time ~12sec):</p>
  
  <pre><code>all = {}
  for i in range(1000000):
    o = {}
    o['i'] = i
    o['l'] = []
    all[i] = o
  </code></pre>
  
  <p><strong>Question:</strong>
  Am I doing something wrong or dictionary is just faster than object? If indeed dictionary performs better, can somebody explain why?</p>