原來這是一隻紅鯡魚。增加字節大小的規則在cPython 2.7 - 3.2和cPython 3.3之間以及在cPython 3.4中改變(儘管這種改變只適用於刪除時)。我們可以使用下面的代碼,以確定看到這個當字典擴展:
import sys
size_old = 0
for n in range(512):
d = {i: i for i in range(n)}
size = sys.getsizeof(d)
if size != size_old:
print(n, size_old, size)
size_old = size
的Python 2.7:
(0, 0, 280)
(6, 280, 1048)
(22, 1048, 3352)
(86, 3352, 12568)
的Python 3.5
0 0 288
6 288 480
12 480 864
22 864 1632
44 1632 3168
86 3168 6240
的Python 3.6:
0 0 240
6 240 368
11 368 648
22 648 1184
43 1184 2280
86 2280 4704
請記住,dicts調整w如果它們滿足2/3,我們可以看到cPython 2.7 dict的實現在擴展時增加了四倍,而cPython 3.5/3.6 dict的實現只增加了一倍。
這在dict source code評論解釋:
/* GROWTH_RATE. Growth rate upon hitting maximum load.
* Currently set to used*2 + capacity/2.
* This means that dicts double in size when growing without deletions,
* but have more head room when the number of deletions is on a par with the
* number of insertions.
* Raising this to used*4 doubles memory consumption depending on the size of
* the dictionary, but results in half the number of resizes, less effort to
* resize.
* GROWTH_RATE was set to used*4 up to version 3.2.
* GROWTH_RATE was set to used*2 in version 3.3.0
*/
嗯,從來沒有注意到這一點,很好找。我不確定是否做出了改變,所以普通字典可以從組合表格中受益(請參閱我在此處做過的問答)(https://stackoverflow.com/questions/42419011/why-is-the-dict- python-3)在實例dicts上),但它可能是值得研究的。我懷疑它,但:-) –