c - 对 murmurhash3 提出改进建议

标签 c hash murmurhash

我只想对 64 位整数进行哈希处理。我正在使用 murmurhash3 的实现,给出 here 。考虑到这个限制,代码是否可以进行一些改进?我无法完全弄清楚,但我认为第 171 行的 for 循环可能是目标。 请就此提出一些建议。

最佳答案

如果您只需要散列 64 位数字,则使用数字值,因为 murmur3 所做的所有操作都是浪费 CPU 周期,将相同的输入数字混合到相同的输出数字,唯一的异常(exception)是如果您要更改种子。

如果您确实想针对固定大小进行优化,您可以复制该函数,然后稍微更改它(允许编译器常量传播和常量折叠来完成繁重的工作):

void MurmurHash3_x86_128_uint64 ( const void * key, uint32_t seed, void * out)
{
  const int len = sizeof(uint64_t); 
  //now len is a compile time constant, and can be folded when this
  //function is not inlined (else it would just be a constant input,
  //which could only be folded when the function is inlined)
  const uint8_t * data = (const uint8_t*)key;
  const int nblocks = len / 16;

如果您在以后的任何阶段使用 C++,那么将其转换为模板是有意义的:

template<const size_t len>
void MurmurHash3_x86_128_uint64 ( const void * key, uint32_t seed, void * out)
{
  const uint8_t * data = (const uint8_t*)key;
  const int nblocks = len / 16;

另请注意,一些更智能的编译器(ICC、MSVC、GCC)将检测函数是否仅使用相同的常量参数(包括部分常量参数列表)调用,并将这些常量折叠到函数中(这需要“整个程序优化”选项已启用)

关于c - 对 murmurhash3 提出改进建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11046067/

相关文章:

C++ Builder - 将 TIdHashSHA256 与 OpenSSL 结合使用

将数组的一部分复制到另一个变量中

c - 检查字符串是否是c中的数字时出现问题

objective-c - 在 iOS 应用程序中编写 C 代码时出现 EXC BAD ACCESS

c - ZLIB 似乎在 C 中返回 CRC32B 而不是 CRC32

algorithm - 具有复合键的 Cassandra 哈希算法

arrays - 尝试使用指针获取 2 2D 数组的差异

javascript - 检查 jQuery 或 Javascript 中哈希数组中某些值的存在

hash - Cassandra,优化 in 子句

node.js - 使用 Apache MurmurHash3.java x86 32 位方法具有负值