简体   繁体   English

在C ++中,对于unordered_map / set是否有更快的哈希函数?

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

Default function is from std::hash. 默认函数来自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. 我尝试使用Google的City Hash的整数和字符串键,但其性能比std :: hash差一些。

std::hash functions are already good in performance. std :: hash函数的性能已经不错。 I think you should try open source hash functions. 我认为您应该尝试开源哈希函数。

Check this out https://github.com/Cyan4973/xxHash . 检查一下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)." 我从其描述中引用:“ xxHash是一种非常快速的Hash算法,以RAM速度限制运行。它成功完成了SMHasher测试套件,该套件评估了哈希函数的冲突,分散和随机性。代码具有高度的可移植性,并且哈希在所有平台(小/大端)。”

Also this thread from another question on this site: Fast Cross-Platform C/C++ Hashing Library . 此线程也来自该站点上的另一个问题: 快速跨平台C / C ++哈希库 FNV, Jenkins and MurmurHash are known to be fast. FNV,Jenkins和MurmurHash速度很快。

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. 还要注意,内置的哈希函数针对不同类型的构建方式也有所不同,因此,我希望对intstring的哈希函数已经在时间和复杂性方面进行了优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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