UPDATE: Oto another, może być bardziej dokładny przepis szacowania rozmiaru obiektu pytona.
Oto thread adresowania podobne pytanie
Proponowane rozwiązanie jest napisanie własnego ... korzystania z niektórych szacunków znanej wielkości prymitywów, obiekt napowietrznych Pythona, oraz wielkość wbudowanych typów kontenerów.
Ponieważ kod nie jest tak długo, tutaj jest bezpośrednią kopią go:
def sizeof(obj):
"""APPROXIMATE memory taken by some Python objects in
the current 32-bit CPython implementation.
Excludes the space used by items in containers; does not
take into account overhead of memory allocation from the
operating system, or over-allocation by lists and dicts.
"""
T = type(obj)
if T is int:
kind = "fixed"
container = False
size = 4
elif T is list or T is tuple:
kind = "variable"
container = True
size = 4*len(obj)
elif T is dict:
kind = "variable"
container = True
size = 144
if len(obj) > 8:
size += 12*(len(obj)-8)
elif T is str:
kind = "variable"
container = False
size = len(obj) + 1
else:
raise TypeError("don't know about this kind of object")
if kind == "fixed":
overhead = 8
else: # "variable"
overhead = 12
if container:
garbage_collector = 8
else:
garbage_collector = 0
malloc = 8 # in most cases
size = size + overhead + garbage_collector + malloc
# Round to nearest multiple of 8 bytes
x = size % 8
if x != 0:
size += 8-x
size = (size + 8)
return size
Duplikat : http://stackoverflow.com/questions/33978/find-out-how-much-memory-is-being-used-by-an-object-in-python, http://stackoverflow.com/questions/512893/pamięć-wykorzystanie-w-dużych-strukturach danych-manipulacja-przetwarzanie –
Związane również: http://stackoverflow.com/questions/13566 4/ile-bajtów-na-element-jest-w-python-lista-tuple/159844 – Constantin
Przepraszam. Chcę tylko zapytać, czy jest jakaś implementacja tej funkcji w ipython, lub moduł do dodawania funkcji "magic" w ipython (Ponieważ używam tego do testowania bardzo dużo.) – Ross