Measure Object Size Accurately in Python - Sys.GetSizeOf not functioning

前端 未结 6 1966
一向
一向 2020-11-30 12:31

I am trying to accurately/definitively find the size differences between two different classes in Python. They are both new style classes, save for one not having sl

6条回答
  •  渐次进展
    2020-11-30 13:22

    You might want to use a different implementation for getting the size of your objects in memory:

    >>> import sys, array
    >>> sizeof = lambda obj: sum(map(sys.getsizeof, explore(obj, set())))
    >>> def explore(obj, memo):
        loc = id(obj)
        if loc not in memo:
            memo.add(loc)
            yield obj
            if isinstance(obj, memoryview):
                yield from explore(obj.obj, memo)
            elif not isinstance(obj, (range, str, bytes, bytearray, array.array)):
                # Handle instances with slots.
                try:
                    slots = obj.__slots__
                except AttributeError:
                    pass
                else:
                    for name in slots:
                        try:
                            attr = getattr(obj, name)
                        except AttributeError:
                            pass
                        else:
                            yield from explore(attr, memo)
                # Handle instances with dict.
                try:
                    attrs = obj.__dict__
                except AttributeError:
                    pass
                else:
                    yield from explore(attrs, memo)
                # Handle dicts or iterables.
                for name in 'keys', 'values', '__iter__':
                    try:
                        attr = getattr(obj, name)
                    except AttributeError:
                        pass
                    else:
                        for item in attr():
                            yield from explore(item, memo)
    
    
    >>> class Test1:
        def __init__(self):
            self.one = 1
            self.two = 'two variable'
    
    
    >>> class Test2:
        __slots__ = 'one', 'two'
        def __init__(self):
            self.one = 1
            self.two = 'two variable'
    
    
    >>> print('sizeof(Test1()) ==', sizeof(Test1()))
    sizeof(Test1()) == 361
    >>> print('sizeof(Test2()) ==', sizeof(Test2()))
    sizeof(Test2()) == 145
    >>> array_test1, array_test2 = [], []
    >>> for _ in range(3000):
        array_test1.append(Test1())
        array_test2.append(Test2())
    
    
    >>> print('sizeof(array_test1) ==', sizeof(array_test1))
    sizeof(array_test1) == 530929
    >>> print('sizeof(array_test2) ==', sizeof(array_test2))
    sizeof(array_test2) == 194825
    >>> 
    

    Just make sure that you do not give any infinite iterators to this code if you want an answer back.

提交回复
热议问题