Optimizing Lookups: Dictionary key lookups vs. Array index lookups

前端 未结 7 1180
忘了有多久
忘了有多久 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 03:52

    The cost of retrieving an element from a Dictionary is O(1), but that's because a dictionary is implemented as a hashtable - so you have to first calculate the hash value to know which element to return. Hashtables are often not that efficient - but they are good for large datasets, or datasets that have a lot of unique-hash values.

    The List (apart from being a rubbish word used to dercribe an array rather than a linked list!) will be faster as it will return the value by directly calculating the element you want returned.

提交回复
热议问题