Which is faster, Hash lookup or Binary search?

后端 未结 17 2506
你的背包
你的背包 2020-12-02 06:47

When given a static set of objects (static in the sense that once loaded it seldom if ever changes) into which repeated concurrent lookups are needed with optimal performanc

17条回答
  •  一整个雨季
    2020-12-02 06:54

    Ok, I'll try to be short.

    C# short answer:

    Test the two different approaches.

    .NET gives you the tools to change your approach with a line of code. Otherwise use System.Collections.Generic.Dictionary and be sure to initialize it with a large number as initial capacity or you'll pass the rest of your life inserting items due to the job GC has to do to collect old bucket arrays.

    Longer answer:

    An hashtable has ALMOST constant lookup times and getting to an item in an hash table in the real world does not just require to compute an hash.

    To get to an item, your hashtable will do something like this:

    • Get the hash of the key
    • Get the bucket number for that hash (usually the map function looks like this bucket = hash % bucketsCount)
    • Traverse the items chain (basically it's a list of items that share the same bucket, most hashtables use this method of handling bucket/hash collisions) that starts at that bucket and compare each key with the one of the item you are trying to add/delete/update/check if contained.

    Lookup times depend on how "good" (how sparse is the output) and fast is your hash function, the number of buckets you are using and how fast is the keys comparer, it's not always the best solution.

    A better and deeper explanation: http://en.wikipedia.org/wiki/Hash_table

提交回复
热议问题