SHARE
TWEET

Untitled

a guest Aug 25th, 2014 374 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. uses dwsUtils, SynCommons, Generics.Defaults;
  2.  
  3. const
  4.    HASH_BITS = 16;
  5.    HASH_MASK = (1 shl HASH_BITS)-1;
  6.  
  7. var
  8.    values : array of String;
  9.    hashes : array [0.. HASH_MASK] of Word;
  10.  
  11. type
  12.    THashFunc = function (const s : String) : Cardinal;
  13.  
  14. function RunHash(func : THashFunc) : String;
  15. var
  16.    i, j, k : Integer;
  17.    mt, t, mx : Cardinal;
  18. begin
  19.    FillChar(hashes, SizeOf(hashes), 0);
  20.    for i:=0 to High(values) do begin
  21.       Inc(hashes[func(values[i]) and HASH_MASK]);
  22.    end;
  23.  
  24.    mt:=MaxInt;
  25.  
  26.    for k:=1 to 30 do begin
  27.       t:=GetTickCount;
  28.  
  29.       for j:=1 to 100 do
  30.          for i:=0 to High(values) do
  31.             func(values[i]);
  32.  
  33.       t:=GetTickCount-t;
  34.       if t<mt then mt:=t;
  35.    end;
  36.  
  37.    j:=0;
  38.    mx:=0;
  39.    for i:=0 to HASH_MASK do begin
  40.       if hashes[i]>1 then
  41.          Inc(j, hashes[i]-1);
  42.       if hashes[i]>mx then
  43.          mx:=hashes[i];
  44.    end;
  45.  
  46.    Result:= 'Duration: '+IntToStr(t)+#13#10
  47.            +'Collisions : '+IntToStr(j)+' ('+IntToStr(mx)+')'+#13#10;
  48. end;
  49.  
  50. function KR32Hash(const s : String) : Cardinal;
  51. begin
  52.    Result:=kr32(0, Pointer(@s[1]), Length(s)*2);
  53. end;
  54.  
  55. function CRC32Hash(const s : String) : Cardinal;
  56. begin
  57.    Result:=crc32cfast(0, Pointer(@s[1]), Length(s)*2);
  58. end;
  59.  
  60. function FNVHash(const s : String) : Cardinal;
  61. begin
  62.    Result:=fnv32(0, Pointer(@s[1]), Length(s)*2);
  63. end;
  64.  
  65. function BJHash(const s : String) : Cardinal;
  66. begin
  67.    Result:=BobJenkinsHash(s[1], Length(s)*2, 0);
  68. end;
  69.  
  70. function ShaHash(const s : String) : Cardinal;
  71. begin
  72.    Result:=StrToInt('$'+Copy(SHA256(@s[1], Length(s)*2), 1, 6));
  73. end;
  74.  
  75. procedure TForm82.FormCreate(Sender: TObject);
  76. var
  77.    i : Integer;
  78. begin
  79.    SetLength(values, 100000);
  80.    for i:=0 to 99999 do
  81.       values[i]:=Format('%.05d', [i]);
  82.  
  83.    Memo1.Lines.Add(
  84.        'SimpleStringHash'#13#10
  85.       +RunHash(SimpleStringHash)
  86.       +#13#10
  87.       +'FNV32'#13#10
  88.       +RunHash(FNVHash)
  89.       +#13#10
  90.       +'KR32'#13#10
  91.       +RunHash(KR32Hash)
  92.       +#13#10
  93.       +'CRC32'#13#10
  94.       +RunHash(CRC32Hash)
  95.       +#13#10
  96.       +'BobJenkins'#13#10
  97.       +RunHash(BJHash)
  98.       +#13#10
  99. //      +'SHA256'#13#10
  100. //      +RunHash(ShaHash)
  101.       +#13#10
  102.       );
  103.  
  104. end;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top