Dictionary vs Object - which is more efficient and why?

后端 未结 8 775
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 15:55

What is more efficient in Python in terms of memory usage and CPU consumption - Dictionary or Object?

Background: I have to load huge amount of data

相关标签:
8条回答
  • 2020-11-29 16:37

    There is yet another way to reduce memory usage if data structure isn't supposed to contain reference cycles.

    Let's compare two classes:

    class DataItem:
        __slots__ = ('name', 'age', 'address')
        def __init__(self, name, age, address):
            self.name = name
            self.age = age
            self.address = address
    

    and

    $ pip install recordclass
    
    >>> from recordclass import structclass
    >>> DataItem2 = structclass('DataItem', 'name age address')
    >>> inst = DataItem('Mike', 10, 'Cherry Street 15')
    >>> inst2 = DataItem2('Mike', 10, 'Cherry Street 15')
    >>> print(inst2)
    >>> print(sys.getsizeof(inst), sys.getsizeof(inst2))
    DataItem(name='Mike', age=10, address='Cherry Street 15')
    64 40
    

    It became possible since structclass-based classes doesn't support cyclic garbage collection, which is not needed in such cases.

    There is also one advantage over __slots__-based class: you are able to add extra attributes:

    >>> DataItem3 = structclass('DataItem', 'name age address', usedict=True)
    >>> inst3 = DataItem3('Mike', 10, 'Cherry Street 15')
    >>> inst3.hobby = ['drawing', 'singing']
    >>> print(inst3)
    >>> print(sizeof(inst3), 'has dict:',  bool(inst3.__dict__))
    DataItem(name='Mike', age=10, address='Cherry Street 15', **{'hobby': ['drawing', 'singing']})
    48 has dict: True
    
    0 讨论(0)
  • 2020-11-29 16:41

    Attribute access in an object uses dictionary access behind the scenes - so by using attribute access you are adding extra overhead. Plus in the object case, you are incurring additional overhead because of e.g. additional memory allocations and code execution (e.g. of the __init__ method).

    In your code, if o is an Obj instance, o.attr is equivalent to o.__dict__['attr'] with a small amount of extra overhead.

    0 讨论(0)
提交回复
热议问题