Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to store big number in list?

Tags:

python

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?

like image 235
Shahriar Avatar asked Dec 18 '14 20:12

Shahriar


People also ask

How do you store large numbers in Python?

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.

How much space does Python list take?

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.

How elements in list are stored in memory in Python?

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.


1 Answers

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.

like image 31
NPE Avatar answered Oct 22 '22 04:10

NPE