Why are dict lookups always better than list lookups?

后端 未结 3 1631
说谎
说谎 2020-12-25 11:21

I was using a dictionary as a lookup table but I started to wonder if a list would be better for my application -- the amount of entries in my lookup table wasn\'t that big.

3条回答
  •  青春惊慌失措
    2020-12-25 11:54

    You say "accessing a few elements in an array is faster than computing a hash".

    A simple hashing rule for strings might be just a sum (with a modulo in the end). This is a branchless operation that can compare favorably with character comparisons, especially when there is a long match on the prefix.

提交回复
热议问题