Size of integer in Python

getsizeof int

In some programming language and in numpy as well, you have some pre-defined integer types taking up 8, 16, or even 256 bits of memory. Accordingly they can hold integer numbers to the appropriate exponent of 2. So an 8-bit unsigned integer uses 8 bits (1 byte) and can hold values between 0-255 (that is 2**8-1).

Python hides the need to make a decision and a variable in Python can hold an arbitrarily large integer. This, however comes at a price. The following little program demonstrates, using the powers of 2, that Python can hold very large integers. It also shows that even for a small integer such as 1, Python uses 28 bytes(!) very wastefull compared to other programming languages and to numpy. It also demonstrats how the used memory will grow as the number grows.

examples/size_of_integer.py

import sys


print(f"   i size len                                               2**i")
for i in range(0, 261, 10):
    number = 2**i
    print(f"{i:4} {sys.getsizeof(number):4} {len(str(number)):3} {number:100}")

  • i - the power
  • size - size of the variable in bytes
  • len - number of digits in the value (which power of 10 is this value)
  • 2**i - the actually number

examples/size_of_integer.txt

   i size len                                               2**i
   0   28   1                                                                                                    1
  10   28   4                                                                                                 1024
  20   28   7                                                                                              1048576
  30   32  10                                                                                           1073741824
  40   32  13                                                                                        1099511627776
  50   32  16                                                                                     1125899906842624
  60   36  19                                                                                  1152921504606846976
  70   36  22                                                                               1180591620717411303424
  80   36  25                                                                            1208925819614629174706176
  90   40  28                                                                         1237940039285380274899124224
 100   40  31                                                                      1267650600228229401496703205376
 110   40  34                                                                   1298074214633706907132624082305024
 120   44  37                                                                1329227995784915872903807060280344576
 130   44  40                                                             1361129467683753853853498429727072845824
 140   44  43                                                          1393796574908163946345982392040522594123776
 150   48  46                                                       1427247692705959881058285969449495136382746624
 160   48  49                                                    1461501637330902918203684832716283019655932542976
 170   48  52                                                 1496577676626844588240573268701473812127674924007424
 180   52  55                                              1532495540865888858358347027150309183618739122183602176
 190   52  58                                           1569275433846670190958947355801916604025588861116008628224
 200   52  61                                        1606938044258990275541962092341162602522202993782792835301376
 210   56  64                                     1645504557321206042154969182557350504982735865633579863348609024
 220   56  67                                  1684996666696914987166688442938726917102321526408785780068975640576
 230   56  70                               1725436586697640946858688965569256363112777243042596638790631055949824
 240   60  73                            1766847064778384329583297500742918515827483896875618958121606201292619776
 250   60  76                         1809251394333065553493296640760748560207343510400633813116524750123642650624
 260   60  79                      1852673427797059126777135760139006525652319754650249024631321344126610074238976

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Python Maven web site.

Gabor Szabo