建议murmurhash3的改进

时间:2012-06-15 07:04:52

标签: c hash murmurhash

我只想散列64位整数。我正在使用here给出murmurhash3的实现。在给定此约束的情况下,代码是否可以有一些改进。我无法完全理解它,但我认为第171行的for循环可能是目标。 请提出一些建议。

1 个答案:

答案 0 :(得分:2)

如果你只需要哈希64位数字,那么使用数字值,因为所有murmur3都会浪费CPU周期将相同的输入数字混合到相同的输出数字,唯一的例外是你要更改种子。

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

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)将检测是否仅使用相同的常量参数(包括部分常量参数列表)调用该函数,并将这些常量折叠到函数中(这需要“整个程序优化“启用选项”

相关问题