Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uses dwsUtils, SynCommons, Generics.Defaults;
- const
- HASH_BITS = 16;
- HASH_MASK = (1 shl HASH_BITS)-1;
- var
- values : array of String;
- hashes : array [0.. HASH_MASK] of Word;
- type
- THashFunc = function (const s : String) : Cardinal;
- function RunHash(func : THashFunc) : String;
- var
- i, j, k : Integer;
- mt, t, mx : Cardinal;
- begin
- FillChar(hashes, SizeOf(hashes), 0);
- for i:=0 to High(values) do begin
- Inc(hashes[func(values[i]) and HASH_MASK]);
- end;
- mt:=MaxInt;
- for k:=1 to 30 do begin
- t:=GetTickCount;
- for j:=1 to 100 do
- for i:=0 to High(values) do
- func(values[i]);
- t:=GetTickCount-t;
- if t<mt then mt:=t;
- end;
- j:=0;
- mx:=0;
- for i:=0 to HASH_MASK do begin
- if hashes[i]>1 then
- Inc(j, hashes[i]-1);
- if hashes[i]>mx then
- mx:=hashes[i];
- end;
- Result:= 'Duration: '+IntToStr(t)+#13#10
- +'Collisions : '+IntToStr(j)+' ('+IntToStr(mx)+')'+#13#10;
- end;
- function KR32Hash(const s : String) : Cardinal;
- begin
- Result:=kr32(0, Pointer(@s[1]), Length(s)*2);
- end;
- function CRC32Hash(const s : String) : Cardinal;
- begin
- Result:=crc32cfast(0, Pointer(@s[1]), Length(s)*2);
- end;
- function FNVHash(const s : String) : Cardinal;
- begin
- Result:=fnv32(0, Pointer(@s[1]), Length(s)*2);
- end;
- function BJHash(const s : String) : Cardinal;
- begin
- Result:=BobJenkinsHash(s[1], Length(s)*2, 0);
- end;
- function ShaHash(const s : String) : Cardinal;
- begin
- Result:=StrToInt('$'+Copy(SHA256(@s[1], Length(s)*2), 1, 6));
- end;
- procedure TForm82.FormCreate(Sender: TObject);
- var
- i : Integer;
- begin
- SetLength(values, 100000);
- for i:=0 to 99999 do
- values[i]:=Format('%.05d', [i]);
- Memo1.Lines.Add(
- 'SimpleStringHash'#13#10
- +RunHash(SimpleStringHash)
- +#13#10
- +'FNV32'#13#10
- +RunHash(FNVHash)
- +#13#10
- +'KR32'#13#10
- +RunHash(KR32Hash)
- +#13#10
- +'CRC32'#13#10
- +RunHash(CRC32Hash)
- +#13#10
- +'BobJenkins'#13#10
- +RunHash(BJHash)
- +#13#10
- // +'SHA256'#13#10
- // +RunHash(ShaHash)
- +#13#10
- );
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement