Skip to content

Instantly share code, notes, and snippets.

@TonyDiana
Last active June 12, 2021 20:06
Show Gist options
  • Select an option

  • Save TonyDiana/9fcbef68f413144fe3055195c629aea7 to your computer and use it in GitHub Desktop.

Select an option

Save TonyDiana/9fcbef68f413144fe3055195c629aea7 to your computer and use it in GitHub Desktop.

Revisions

  1. Tony Diana revised this gist Jun 12, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions memocup.py
    Original 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
    ---------------------------------------------------------------------------
    """
  2. Tony Diana revised this gist Jun 12, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion memocup.py
    Original 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)])

  3. Tony Diana revised this gist Jun 12, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions memocup.py
    Original 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)

  4. Tony Diana revised this gist Jun 12, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion memocup.py
    Original 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 śi lo es
    # muy elegante de manejar la recursividad, y lo cierto es que lo es
    almacen.add(obj_id)

    # --- Consultar el tamaño de ese elemento, según su tipo
  5. Tony Diana created this gist Jun 12, 2021.
    42 changes: 42 additions & 0 deletions memocup.py
    Original 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