Python dictionary iterator performance

二次信任 提交于 2019-12-01 17:17:48

If you look at the notes on Python's dictionary source code, I think the relevant points are the following:

Those methods (iteration and key listing) loop over every potential entry

How many potential entries will there be, as a function of largest size (largest number of keys ever stored in that dictionary)? Look at the following two sections in the same document:

Maximum dictionary load in PyDict_SetItem. Currently set to 2/3

Growth rate upon hitting maximum load. Currently set to *2.

This would suggest that the sparsity of a dictionary is going to be somewhere around 1/3~2/3 (unless growth rate is set to *4, then it's 1/6~2/3). So basically you're going to be checking upto 3 (or 6 if *4) potential entries for every key.

Of course, whether it's 3 entries or 1000, it's still O(n) but 3 seems like a pretty acceptable constant factor.

Incidentally here are the rest of the source & documentation, including that of the DictObject:

http://svn.python.org/projects/python/trunk/Objects/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!