python: class vs tuple huge memory overhead (?)

后端 未结 4 942
萌比男神i
萌比男神i 2021-02-20 12:16

I\'m storing a lot of complex data in tuples/lists, but would prefer to use small wrapper classes to make the data structures easier to understand, e.g.

class Pe         


        
4条回答
  •  醉梦人生
    2021-02-20 12:48

    There is yet another way to reduce the amount of memory occupied by objects by turning off support for cyclic garbage collection in addition to turning off __dict__ and __weakref__. It is implemented in the library recordclass:

    $ pip install recordclass
    
    >>> import sys
    >>> from recordclass import dataobject, make_dataclass
    

    Create the class:

    class Person(dataobject):
       first:str
       last:str
    

    or

    >>> Person = make_dataclass('Person', 'first last')
    

    As result:

    >>> print(sys.getsizeof(Person(100,100)))
    32
    

    For __slot__ based class we have:

    class Person:
        __slots__ = ['first', 'last']
        def __init__(self, first, last):
            self.first = first
            self.last = last
    
    >>> print(sys.getsizeof(Person(100,100)))
    64
    

    As a result more saving of memory is possible.

    For dataobject-based:

    l = [Person(i, i) for i in range(10000000)]
    memory size: 681 Mb
    

    For __slots__-based:

      l = [Person(i, i) for i in range(10000000)]
      memory size: 921 Mb
    

提交回复
热议问题