Optimizing Lookups: Dictionary key lookups vs. Array index lookups

前端 未结 7 1179
忘了有多久
忘了有多久 2020-12-15 03:40

I\'m writing a 7 card poker hand evaluator as one of my pet projects. While trying to optimize its speed (I like the challenge), I was shocked to find that the performance o

7条回答
  •  天涯浪人
    2020-12-15 04:02

    An array lookup is about the fastest thing you can do - essentially all it is is a single bit of pointer arithmetic to go from the start of the array to the element you wanted to find. On the other hand, the dictionary lookup is likely to be somewhat slower since it needs to do hashing and concern itself with finding the correct bucket. Although the expected runtime is also O(1) - the algorithmic constants are greater so it will be slower.

提交回复
热议问题