How do Python dictionary hash lookups work?

前端 未结 5 2154
轮回少年
轮回少年 2020-12-13 14:42

How do Python dictionary lookup algorithms work internally?

mydi[\'foo\'] 

If the dictionary has 1,000,000 terms, is a tree search execut

5条回答
  •  时光说笑
    2020-12-13 15:15

    Answer 1: Internal working is explained in this video

    Answer 2: No, a tree search is not done if you have a million records in a dictionary.

    Answer 3: As there might be key collisions you will expect performance in terms of the size of the dictionary, and not in terms of the length of the key string.

    Answer 4: Consider the dictionary as an array (contiguous memory locations), but there might be blocks within the array which are not used. Hence, dictionaries tend to waste a lot of memory space as compared to trees. But, for better run-time performance dictionaries might be better than trees. Key collisions can sometime degrade performance. You should read about Consistent Hashing.

提交回复
热议问题