0

我只想散列 64 位整数。我正在使用此处给出的 murmurhash3 的实现。鉴于此约束,代码是否可以进行一些改进。我无法完全弄清楚,但我认为第 171 行的 for 循环可能是目标。请就此提出一些建议。

4

1 回答 1

2

如果您只需要散列 64 位数字,则使用 numbers 值,因为所有 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)将检测函数是否仅使用相同的常量参数(包括部分常量参数列表)调用并将这些常量折叠到函数中(这需要“整个程序优化" 选项被启用)

于 2012-06-15T07:16:26.470 回答