Hashtable is that fast

Posted by Costa on Stack Overflow See other posts from Stack Overflow or by Costa
Published on 2010-04-02T06:15:22Z Indexed on 2010/04/02 6:23 UTC
Read the original article Hit count: 272

Filed under:
|
|
|
|

Hi

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]. Is the hash function of the java string, I assume the rest of languages is similar or close to this implementation.

If we have hash-Table and a list of 50 elements. each element is 7 chars ABCDEF1, ABCDEF2, ABCDEF3..... ABCDEFn

If each bucket of hashtable contains 5 strings (I think this function will make it one string per bucket, but let us assume it is 5).

If we call col.Contains("ABCDEFn"); // will do 6 comparisons and discover the difference on the 7th.

The hash-table will take around 70 operations (multiplication and additions) to get the hashcode and to compare with 5 strings in bucket. and BANG it found.

For list it will take around 300 comparisons to find it.

for the case that there is only 10 elements, the list will take around 70 operations but the Hashtable will take around 50 operations. and note that hashtable operations are more time consuming (it is multiplications).

I conclude that HybirdDictionary in .Net probably is the best choice for that most cases that require Hashtable with unknown size, because it will let me use a list till the list becomes more than 10 elements. still need something like HashSet rather than a Dictionary of keys and values, I wonder why there is no HybirdSet!!

So what do u think?

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET