大型python dict中的数据结构的性能损失

This article is categorized as "Garbage" . It should NEVER be appeared in your search engine's results.

当python dict存储元素过多时,无论是insert还是query都会体验到明显的性能下降(理论上是O(1),实际上并不完全是)

原因大概是哈希碰撞:🔗 [Why can a Python dict have multiple keys with the same hash? - Stack Overflow] https://stackoverflow.com/questions/9010222/why-can-a-python-dict-have-multiple-keys-with-the-same-hash

性能下降的时机可能和dict存储的key的类型有关,(猜测)结构复杂的key更容易提前引发性能下降,和python dict的hash计算方式有关:🔗 [data structures - How are Python's Built In Dictionaries Implemented? - Stack Overflow] https://stackoverflow.com/questions/327311/how-are-pythons-built-in-dictionaries-implemented/9022835

写了个小程序验证,其中dict的key是这种格式的:

dict['1000->1001'] = value

实际验证下来,存储100万个元素时,query的速度下降了1~2倍。insert的速度应该也是更慢的,但就没写代码去测试了。


 Last Modified in 2025-06-26 

Leave a Comment Anonymous comment is allowed / 允许匿名评论