Last active
June 12, 2021 20:06
-
-
Save TonyDiana/9fcbef68f413144fe3055195c629aea7 to your computer and use it in GitHub Desktop.
Revisions
-
Tony Diana revised this gist
Jun 12, 2021 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,8 @@ :Autor: https://goshippo.com/blog/measure-real-size-any-python-object/ :Versión: Desconocido :Adaptación: Tony Diana Se ha mejorado y se añadió medir también los atributos de los objetos --------------------------------------------------------------------------- """ -
Tony Diana revised this gist
Jun 12, 2021 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,7 +33,6 @@ def get_size(obj, almacen=None): size += sum([get_size(v, almacen) for v in obj.values()]) size += sum([get_size(k, almacen) for k in obj.keys()]) elif isinstance(obj, object): size += sum([get_size(v, almacen) for v in dir(obj)]) -
Tony Diana revised this gist
Jun 12, 2021 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,6 +33,10 @@ def get_size(obj, almacen=None): size += sum([get_size(v, almacen) for v in obj.values()]) size += sum([get_size(k, almacen) for k in obj.keys()]) elif isinstance(obj, object): size += sum([get_size(v, almacen) for v in dir(obj)]) elif hasattr(obj, '__dict__'): size += get_size(obj.__dict__, almacen) -
Tony Diana revised this gist
Jun 12, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,7 +25,7 @@ def get_size(obj, almacen=None): return 0 # Añadir el ID del objeto al almacén. Según el autor original, una manera # muy elegante de manejar la recursividad, y lo cierto es que sí lo es almacen.add(obj_id) # --- Consultar el tamaño de ese elemento, según su tipo -
Tony Diana created this gist
Jun 12, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ # -*- coding: utf-8 *-* """ :Propósito: Medir la memoria consumida por un objeto Python :Autor: https://goshippo.com/blog/measure-real-size-any-python-object/ :Versión: Desconocido :Adaptación: Tony Diana --------------------------------------------------------------------------- """ import sys def get_size(obj, almacen=None): """ Medir recursivamente la ocupación en memoria de un objeto Python. """ size = sys.getsizeof(obj) # --- Establecer el almacén de datos if almacen is None: almacen = set() # --- Consultar si el objeto ya se encuentra en el almacén, para no sumarlo obj_id = id(obj) if obj_id in almacen: return 0 # Añadir el ID del objeto al almacén. Según el autor original, una manera # muy elegante de manejar la recursividad, y lo cierto es que śi lo es almacen.add(obj_id) # --- Consultar el tamaño de ese elemento, según su tipo if isinstance(obj, dict): size += sum([get_size(v, almacen) for v in obj.values()]) size += sum([get_size(k, almacen) for k in obj.keys()]) elif hasattr(obj, '__dict__'): size += get_size(obj.__dict__, almacen) elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)): size += sum([get_size(i, almacen) for i in obj]) return size