MISE À JOUR: Voici another, recette peut-être plus approfondie pour estimer la taille d'un objet python.
Voici une thread aborder une question similaire
La solution proposée est d'écrire votre propre ... en utilisant des estimations de la taille connue des primitives, les frais généraux d'objets de python, et les tailles de construction dans les types de conteneurs.
Étant donné que le code n'est pas si longtemps, voici une copie directe de celui-ci:
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
en double : http://stackoverflow.com/questions/33978/find-out-how-much-memory-is-being-used-by-an-object-in-python, http://stackoverflow.com/questions/512893/memory-use-in-large-data-structures-manipulation-processing –
Egalement en rapport: http://stackoverflow.com/questions/13566 4/comment-beaucoup-octets-par-élément-sont-là-dans-un-python-list-tuple/159844 – Constantin
Désolé. Je veux juste demander s'il y a une implémentation de ces fonctionnalités dans ipython, ou un module pour cela en ajoutant la fonction "magic" dans ipython (puisque je l'utilise pour tester beaucoup.) – Ross