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

六月ゝ 毕业季﹏ 提交于 2019-12-24 05:48:44

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/42336779/are-there-faster-hash-functions-for-unordered-map-set-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!