The previous article talked about __slots__ to reduce memory usage
Namedtuple can also be similar, that is, replace class with collections.namedtuple
The specific code is as follows:
from pympler.asizeof import asizesof
import collections
DaGongRen = collections.namedtuple('DaGongRen', ['id_dg', 'age','salary'])
#@profile
def test():
d= DaGongRen(10001,18,2000)
#d=[ DaGongRen(10001,18,2000) for i in range(100000) ]
#print(asizesof(d.__dict__))
print(asizesof(d))
if __name__ == '__main__':
test()
operation result:
[aspiree1431 opt]# python lru_cache.py
(160,)
The running result is compared with the previous one, and the result is similar to __slots__:
the way | Occupied memory (bytes) |
Not optimized | 416 |
__slots__ | 152 |
namedtuple | 160 |
The remaining functions are similar to the previous one, so I won’t experiment here.
Finally list the previous series:
Fall in love with the python series-python performance (eight): __slots__ reduce memory usage