And much more (because everything in Python is a object and thus - everything is of some specific type).
Sizes of the object of basic types:
import sys, rich
table = rich.table.Table(rich.table.Column(header="Object", style="blue"),
"Type",
rich.table.Column(header="Size", style="cyan", justify="right"),
title="Basic types memory usage", show_lines=True)
objects = [ None, True, 42, 3.1415, "Hello", [1, 2, 3], ("a", "b", "c"),
{1, 2, 3}, frozenset({1, 2, 3}), {"x": 1, "y": 2} ]
for obj in objects:
table.add_row(*map(str, (obj, type(obj), sys.getsizeof(obj))))
rich.console.Console().print(table)
Basic types memory usage
ββββββββββββββββββββββββ³ββββββββββββββββββββββ³βββββββ
β Object β Type β Size β
β‘ββββββββββββββββββββββββββββββββββββββββββββββββββββ©
β None β <class 'NoneType'> β 16 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β True β <class 'bool'> β 28 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β 42 β <class 'int'> β 28 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β 3.1415 β <class 'float'> β 24 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β Hello β <class 'str'> β 54 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β [1, 2, 3] β <class 'list'> β 80 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β ('a', 'b', 'c') β <class 'tuple'> β 64 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β {1, 2, 3} β <class 'set'> β 216 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β frozenset({1, 2, 3}) β <class 'frozenset'> β 216 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β {'x': 1, 'y': 2} β <class 'dict'> β 232 β
ββββββββββββββββββββββββ΄ββββββββββββββββββββββ΄βββββββ
Memory size may not be the only criteria to select data type. Rather, time required to perform operation on data type can be critical criteria.
import sys, rich
n = 1000000
tests = {
"list": [*range(n)],
"tuple": tuple(range(n)),
"set": set(range(n)),
"dict": dict.fromkeys(range(n))
}
table = rich.table.Table("Type",
rich.table.Column(header="Total size", style="cyan", justify="right"),
rich.table.Column(header="Size per element", style="red", justify="right"),
title=f"Collections memory requirement ({n} elements)", show_lines=True
)
for type_, collection in tests.items():
# print(f"Size of <{type_}> ({n} items): {sys.getsizeof(collection)}, {sys.getsizeof(collection) / n} per element")
table.add_row(str(type_), str(sys.getsizeof(collection)), str(sys.getsizeof(collection) / n))
rich.console.Console().print(table)
Collections memory requirement (1000000
elements)
βββββββββ³βββββββββββββ³βββββββββββββββββββ
β Type β Total size β Size per element β
β‘ββββββββββββββββββββββββββββββββββββββββ©
β list β 8000056 β 8.000056 β
βββββββββΌβββββββββββββΌβββββββββββββββββββ€
β tuple β 8000040 β 8.00004 β
βββββββββΌβββββββββββββΌβββββββββββββββββββ€
β set β 33554648 β 33.554648 β
βββββββββΌβββββββββββββΌβββββββββββββββββββ€
β dict β 41943136 β 41.943136 β
βββββββββ΄βββββββββββββ΄βββββββββββββββββββ