Is `namedtuple` really as efficient in memory usage as tuples? My test says NO

前端 未结 2 1054
一整个雨季
一整个雨季 2021-02-12 21:31

It is stated in the Python documentation that one of the advantages of namedtuple is that it is as memory-efficient as tuples.

To validate this, I

2条回答
  •  情深已故
    2021-02-12 21:57

    Doing some investigation myself (with Python 3.6.6). I run into following conclusions:

    1. In all three cases (list of tuples, list of named tuples, list of dicts). sys.getsizeof returns size of the list, which stores only references anyway. So you get size: 81528056 in all three cases.

    2. Sizes of elementary types are:

      sys.getsizeof((1,2,3)) 72

      sys.getsizeof(point(x=1, y=2, z=3)) 72

      sys.getsizeof(dict(x=1, y=2, z=3)) 240

    3. timing is very bad for named tuple:
      list of tuples: 1.8s
      list of named tuples: 10s
      list of dicts: 4.6s

    4. Looking to system load I become suspicious about results from getsizeof. After measuring the footprint of the Ptyhon3 process I get:

      test_list = [(i, i+1, i+2) for i in range(10000000)]
      increase by: 1 745 564K
      that is about 175B per element

      test_list_n = [point(x=i, y=i+1, z=i+2) for i in range(10000000)]
      increase by: 1 830 740K
      that is about 183B per element

      test_list_n = [point(x=i, y=i+1, z=i+2) for i in range(10000000)]
      increase by: 2 717 492 K
      that is about 272B per element

提交回复
热议问题