Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best hashing algorithm to use on a stl string when using hash_map?

I've found the standard hashing function on VS2005 is painfully slow when trying to achieve high performance look ups. What are some good examples of fast and efficient hashing algorithms that should void most collisions?

like image 835
PiNoYBoY82 Avatar asked Sep 18 '08 23:09

PiNoYBoY82


People also ask

Which hashing algorithm is recommended for the?

Probably the one most commonly used is SHA-256, which the National Institute of Standards and Technology (NIST) recommends using instead of MD5 or SHA-1. The SHA-256 algorithm returns hash value of 256-bits, or 64 hexadecimal digits.

What hash function does STL use?

unordered_map hash_function() function in C++ STL The unordered_map::hash_function() is a built in function in C++ STL which is used to get the hash function. This hash function is a unary function which takes a single argument only and returns a unique value of type size_t based on it.

Which hashing algorithm is the most secure?

Common attacks like brute force attacks can take years or even decades to crack the hash digest, so SHA-2 is considered the most secure hash algorithm.

Which hashing technique provides good performance?

Explanation: Universal hashing scheme provides better performance than other schemes because it uses a unique randomisation approach.


1 Answers

I worked with Paul Larson of Microsoft Research on some hashtable implementations. He investigated a number of string hashing functions on a variety of datasets and found that a simple multiply by 101 and add loop worked surprisingly well.

unsigned int hash(     const char* s,     unsigned int seed = 0) {     unsigned int hash = seed;     while (*s)     {         hash = hash * 101  +  *s++;     }     return hash; } 
like image 90
George V. Reilly Avatar answered Sep 19 '22 04:09

George V. Reilly