简体   繁体   中英

Are there faster hash functions for unordered_map/set in C++?

Default function is from std::hash. I wonder if there are better hash functions for saving computational time? for integer keys as well as string keys.

I tried City Hash from Google for both integer and string keys, but its performance is a little worse than std::hash.

std::hash functions are already good in performance. I think you should try open source hash functions.

Check this out https://github.com/Cyan4973/xxHash . I quote from its description: "xxHash is an Extremely fast Hash algorithm, running at RAM speed limits. It successfully completes the SMHasher test suite which evaluates collision, dispersion and randomness qualities of hash functions. Code is highly portable, and hashes are identical on all platforms (little / big endian)."

Also this thread from another question on this site: Fast Cross-Platform C/C++ Hashing Library . FNV, Jenkins and MurmurHash are known to be fast.

You need to explain 'better' in what sense? The fastest hash function would be simply use the value, but that is useless. A more specific answer would depend on your memory constraints and what probabilities of collision are you willing to accept.

Also note that the inbuilt hash functions are built differently for different types, and as a result, I expect the hash functions for int and string to already by optimised in the general sense for time complexity and collision probability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM