program app;
{$mode objfpc}{$h+}
{.$optimization on}
uses
SysUtils, DateUtils,
Generics.Hashes,
Generics.Collections,
ghashmap,
Contnrs,
uPerfectHash,
LgArrayHelpers, LgHash, LgHashMap;
type
TPerfHash = specialize TPerfectHash<string, string>;
TDict = specialize TDictionary<string, string>;
THMHash = class
class function Hash(const Data: String; n: SizeInt): UInt32; static; inline;
end;
TLgHash = class
class function HashCode(const s: string): SizeInt; static; inline;
class function Equal(const L, R: string): Boolean; static; inline;
end;
class function THMHash.Hash(const Data: String; n: SizeInt): UInt32;
begin
Result:=mORMotHasher(0, @Data[1], Length(Data)) and (n-1);
end;
class function TLgHash.HashCode(const s: string): SizeInt;
begin
Result := FNV1A_JesteressM(Pointer(s), Length(s));
end;
class function TLgHash.Equal(const L, R: string): Boolean;
begin
Result := L = R;
end;
type
TGHashMap = ghashmap.specialize THashmap<string, string, THMHash>;
TMapType = specialize TGLiteChainHashMap<string, string, TLgHash>;
const
//N = 32*1024*1024;
N = 3*1024*1024;
var
PerfHash: TPerfHash;
Dict: TDict;
GHM: TGHashMap;
ContnrsSHT: Contnrs.TFPStringHashTable;
LgMap: TMapType.TMap;
i: UInt32;
j: Int32;
t: TDateTime;
s: string;
StrArr: array of string = (
'type','path','lines','line_number','submatches','start','end','elapsed','human',
'matched_lines','matches','searches','searches_with_match'
);
Test: array of string;
begin
Dict:=TDict.Create;
for s in StrArr do
Dict.Add(s, s);
GHM:=TGHashMap.Create;
for s in StrArr do
GHM.insert(s, s);
ContnrsSHT:=Contnrs.TFPStringHashTable.Create;
for s in StrArr do
ContnrsSHT.Add(s, s);
for s in StrArr do
LgMap.Add(s, s);
PerfHash:=TPerfHash.Create;
PerfHash.Generate([
['type', 'type'],
['path', 'path'],
['lines', 'lines'],
['line_number', 'line_number'],
['submatches', 'submatches'],
['start', 'start'],
['end', 'end'],
['elapsed', 'elapsed'],
['human', 'human'],
['matched_lines', 'matched_lines'],
['matches', 'matches'],
['searches', 'searches'],
['searches_with_match', 'searches_with_match']
]);
Randomize;
Test := specialize TGArrayHelpUtil<string>.CreateRandomShuffle(StrArr);
t:=Now;
for i:=0 to N do
for j := 0 to High(Test) do
Dict.TryGetValue(Test[j], s);
WriteLn('Dict: ', MilliSecondsBetween(Now, t), 'ms');
t:=Now;
for i:=0 to N do
for j := 0 to High(Test) do
GHM.GetValue(Test[j], s);
WriteLn('GHM: ', MilliSecondsBetween(Now, t), 'ms');
t:=Now;
for i:=0 to N do
for j := 0 to High(Test) do
s:=ContnrsSHT.Items[Test[j]];
WriteLn('ContnrsSHT: ', MilliSecondsBetween(Now, t), 'ms');
t:=Now;
for i:=0 to N do
for j := 0 to High(Test) do
LgMap.TryGetValue(Test[j], s);
WriteLn('LgMap: ', MilliSecondsBetween(Now, t), 'ms');
t:=Now;
for i:=0 to N do
for j := 0 to High(Test) do
s:=PerfHash.GetValue(Test[j])^;
WriteLn('PerfHash: ', MilliSecondsBetween(Now, t), 'ms');
ReadLn;
end.