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?
Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate.
When you create a list object, the list object by itself takes 64 bytes of memory, and each item adds 8 bytes of memory to the size of the list because of references to other objects.
Python has a built-in module named 'array' which is similar to arrays in C or C++. In this container, the data is stored in a contiguous block of memory. Just like arrays in C or C++, these arrays only support one data type at a time, therefore it's not heterogenous like Python lists.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With