【发布时间】:2015-06-20 16:17:06
【问题描述】:
是否有 MurMurHash 3 的任何 Delphi 实现?我尝试自己实现它,但我的实现实际上比MurMurHash2 慢。正常吗? 还有其他实现吗?
这是我的:
function MurMur3_32(const S: AnsiString; const Seed: LongWord=$9747b28c): LongWord;
const
c1 = $cc9e2d51;
c2 = $1b873593;
r1 = 15;
r2 = 13;
m = 5;
n = $e6546b64;
var
hash: LongWord;
len: LongWord;
k, k2: LongWord;
data: Integer;
begin
Hash := Seed;
len := Length(S);
//The default seed, $9747b28c, is from the original C library
// Initialize the hash to a 'random' value
hash := seed xor len;
// Mix 4 bytes at a time into the hash
data := 1;
while(len >= 4) do
begin
k := PLongWord(@S[data])^;
k := k*c1;
k := (k shl r1) or (k shr (32 - r1));
k := k*c2;
hash := hash xor k;
hash := ((hash shl r2) or (hash shr (32 - r2))) * m + n;
Inc(Data, 4);
Dec(len, 4);
end;
k2 := 0;
{ Handle the last few bytes of the input array
S: ... $69 $18 $2f
}
Assert(len <= 3);
if len = 3 then
k2 := k2 xor (LongWord(s[data+2]) shl 16);
if len >= 2 then
k2 := k2 xor (LongWord(s[data+1]) shl 8);
if len >= 1 then
begin
k2 := k2 xor (LongWord(s[data]));
k2 := k2 * c1;
k2 := (k2 shl r1) or (k2 shr (32 - r1));
k2 := k2 * c2;
hash := hash xor k2;
end;
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
len := Length(S);
hash := hash xor len;
hash := hash xor (hash shr 16);
hash := hash * $85ebca6b;
hash := hash xor (hash shr 13);
hash := hash * $c2b2ae35;
hash := hash xor (hash shr 16);
Result := hash;
end;
免责声明:我不知道Seed 的值是否正确。
【问题讨论】:
-
这个问题可能是关闭的主题和不清楚你问什么。我的主要评论是想知道为什么要将二进制数据存储在字符串中?使用字节数组。
-
我用字符串制作它只是为了尝试实现......当然每个人都会根据自己的需要调整它。我需要对字符串进行哈希处理...您对这个问题有什么不明白的地方?
-
我理解这个问题,但它是题外话,因为你要求其他图书馆和不清楚(技术术语),因为你问一个模糊的“它可以改进”。同样,为什么将二进制数据放在字符串中?如果字符串经过编码转换会发生什么?
-
我认为核心问题很好:
why is my straightforward implementation slow?。当然,混合其他诸如give me other implementations之类的附带问题会使它偏离主题。但我认为这不会影响核心问题。 -
@Johan 这个核心问题不清楚,因为没有提供时间细节
标签: delphi murmurhash