Memory-usage of dictionary in Python?

前端 未结 3 2088
广开言路
广开言路 2021-01-01 15:44

I am slightly confused when I use the getsizeof method in the sys module for dictionaries. Below I have created a simple dictionary of two strings.

3条回答
  •  庸人自扰
    2021-01-01 16:23

    Well, dictionaries don't store the actual string inside them, it works a bit like C/C++ pointers, so you only get a constant overhead in the dictionary for every element.

    The total size is

    size = getsizeof(d)
    size += sum(map(getsizeof, d.itervalues())) + sum(map(getsizeof, d.iterkeys()))
    

提交回复
热议问题