Python字典和设置内存分配 [英] Python dictionary and set memory allocation

查看:43
本文介绍了Python字典和设置内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个优秀的资源讨论 python 字典和集合的内存使用,特别是:

I found this excellent resource discussing memory usage of python dictionaries and sets, specifically:

默认情况下,字典或集合的最小大小是 8(也就是说,如果你只记录 3 个值,python 仍然会分配 8 个元素).在调整大小时,桶的数量增加了 4 倍,直到我们达到 50,000 个元素,之后大小增加了 2 倍.这给出了以下可能的尺寸,

By default, the smallest size of a dictionary or set is 8 (that is, if you are only storying 3 values, python will still allocate 8 elements). On resize, the number of buckets increases by 4x until we reach 50,000 elements, after which the size is increased by 2x. This gives the following possible sizes,

16, 64, 256, 1024, 4096, 16384, 65536, 131072, 262144, ...

16, 64, 256, 1024, 4096, 16384, 65536, 131072, 262144, ...

需要注意的是,调整大小可能会使哈希表变大或变小.也就是说,如果删除足够多的哈希表元素,则可以缩小该表的大小.这是因为表为 2/3rds full 的考虑使用自上次调整大小以来插入和删除的条目总数.但是,调整大小仅在插入期间发生.

It is important to note that resizing can happen to make a hash table larger OR smaller. That is, if sufficiently many elements of a hash table are deleted, the table can be scaled down in size. This is because the consideration for the table being 2/3rds full uses the total of inserted and deleted entries since the last resize. However, resizing only happens during an insert.

但这是在 2014 年 9 月发布的,因此可能是在此之前的某个时间写的.这在最新版本的 Python 中仍然准确和相关吗?(3.6+)

But this was published in September 2014 and thus likely written some time before that. Is this still accurate and relevant in the latest versions of Python? (3.6+)

推荐答案

在 CPython 中,这不再正确.字典实现的这个特定部分已经改变了几次,因为那里写的是这种情况.从这一行开始的评论就在定义的正上方GROWTH_FACTOR 提供了一点历史.

In CPython this is no longer true. This specific part of the dictionary implementation has changed a couple of times since what was written there was the case. The comment starting on this line right above the definition of GROWTH_FACTOR gives a little history.

/* GROWTH_RATE. Growth rate upon hitting maximum load.
 * Currently set to used*3.
 * 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.  See also bpo-17563 and bpo-33205.
 *
 * GROWTH_RATE was set to used*4 up to version 3.2.
 * GROWTH_RATE was set to used*2 in version 3.3.0
 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
 */

书中提供的信息在出版时已经过时了大约两年.

The information provided in the book had been out of date for about two years at the time it was published.

这篇关于Python字典和设置内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆