标签:http os io 使用 ar strong for art cti
O(1)
lookup by index).The figure below is a logical representation of a python hash table. In the figure below, 0, 1, ..., i, ... on the left are indices of the slots in the hash table (they are just for illustrative purposes and are not stored along with the table obviously!).
# Logical model of Python Hash table
-+-----------------+
0| <hash|key|value>|
-+-----------------+
1| ... |
-+-----------------+
.| ... |
-+-----------------+
i| ... |
-+-----------------+
.| ... |
-+-----------------+
n| ... |
-+-----------------+
When a new dict is initialized it starts with 8 slots. (see dictobject.h:49)
i
that is based on the hash of the key. CPython uses initial i = hash(key) & mask
. Where mask = PyDictMINSIZE - 1
, but that‘s not really important). Just note that the initial slot, i, that is checked depends on the hash of the key.<hash|key|value>
). But what if that slot is occupied!? Most likely because another entry has the same hash (hash collision!)==
comparison not the is
comparison) of the entry in the slot against the key of the current entry to be inserted (dictobject.c:337,344-345). If both match, then it thinks the entry already exists, gives up and moves on to the next entry to be inserted. If either hash or the key don‘t match, it startsprobing.Python实现的规则是:
初始情况下,dict的hash table大小为8(PyDict_MINSIZE常量),当dict的hash table使用率达到2/3的时候,就会resize以保证较少的index碰撞。当key的数量小于50k,size*4;当key的数量大于50k,size*2。需要注意的是每次resize的时候,所有的key会被重新插入(从上文的探测算法角度来说,i改变了,index需要重新计算),所以key的顺序很有可能又改变了。
英文部分http://stackoverflow.com/questions/327311/how-are-pythons-built-in-dictionaries-implemented
中文部分http://zhoutall.com/archives/497
具体实现http://www.laurentluce.com/posts/python-dictionary-implementation/
标签:http os io 使用 ar strong for art cti
原文地址:http://www.cnblogs.com/zxpgo/p/3958700.html