20

Is it memory efficient to store big number in list? Why does the following happens?

>>> A = 100**100
>>> sys.getsizeof(A)
102
>>> B = [100**100]
>>> sys.getsizeof(B)
40

Why size of A and B are not equal?

>>> C = [1,100**100]
>>> sys.getsizeof(C)
44
>>> D = [1000**1000, 100**100]
>>> sys.getsizeof(D)
44

Why size of C and D are equal?

Shahriar
  • 13,460
  • 8
  • 78
  • 95

3 Answers3

31

sys.getsizeof() returns the shallow size, i.e. the size of the list object itself but not of the objects it contains.

From the documentation:

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

If you'd like to compute the deep size, it might be worth giving Pympler a try:

>>> from pympler.asizeof import asizeof
>>> A = 100**100
>>> asizeof(A)
120
>>> B = [100**100]
>>> asizeof(B)
200

Thus, on my computer, placing the long inside a list adds 80 bytes of overhead.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
9

The size of A and B are not equal because you've put B into a list. In this case, A is a long and B is a list type. C and D are the same size because their numerical contents are the same width inside the list container.

Further, sys.getsizeof() returns the size of the topmost object using the __sizeof__ method, not the size of the items referenced inside of that object.

  • This explains a lots. Thank you – Shahriar Dec 18 '14 at 20:25
  • @AerofoilKite no problem at all. NPE's answer really nails it head on. In the link to the documentation I gave, there's a link to an ActiveState recipe that details stepping through an object to return the "true" size. –  Dec 18 '14 at 20:26
4

from python wiki :

Its because of that getsizeof() Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

and in this case B is the name of list and actually a pointer to list ! so getsizeof() return the size of a list object , not its content !

Mazdak
  • 105,000
  • 18
  • 159
  • 188