我只想对 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/