Optimizing Lookups: Dictionary key lookups vs. Array index lookups

前端 未结 7 1173
忘了有多久
忘了有多久 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:49

    Is this type of behavior expected (performance decrease by a factor of 8)?

    Why not? Each array lookup is almost intantaneous/negligeable, whereas a dictionary lookup may need at least an extra subroutine call.

    The point of their both being O(1) means that even if you have 50 times more items in each collection, the performance decrease is still only a factor of whatever it is (8).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 04:05

    Something could take a millenium, and still be O(1).

    If you single-step through this code in the disassembly window, you will quickly come to understand what the difference is.

    0 讨论(0)
  • 2020-12-15 04:06

    Dictionary structures are most useful when the key space is very large and cannot be mapped into a stable, sequenced order. If you can convert your keys into a simple integer in a relatively small range, you will be hard-pressed to find a data structure that will perform better than an array.

    On an implementation note; in .NET, dictionaries are essentially hashables. You can somewhat improve their key-lookup performance by ensuring that your keys hash into a large space of unique values. It looks like in your case, you are using a simple integer as a key (which I believe hashes to its own value) - so that may be the best you can do.

    0 讨论(0)
  • Welcome to Big-O notation. You always have to consider that there is a constant factor involved.

    Doing one Dict-Lookup is of course much more expensive than an array lookup.

    Big-O only tells you how algorithms scale. Double the amount of lookups and see how the numbers change: Both should take around the twice time.

    0 讨论(0)
提交回复
热议问题