Hash-Map vs LL
Posted: Fri Jun 01, 2007 7:46 pm
One Debug says more then 1000 words 

Code: Select all
Structure pair
Key.s
Value.l
Belegt.b
EndStructure
start=ElapsedMilliseconds()
Global Mapsize.l=100000
Global Dim map.pair(Mapsize+1);
Procedure.l GetHash(InKey.s);
Protected hash= CRC32Fingerprint(@InKey,StringByteLength(InKey))%mapsize
ProcedureReturn Abs(hash)
EndProcedure
Procedure Add(Key.s, Value.l)
Protected Hash.l
Hash=GetHash(Key.s)
Repeat
If map(hash)\Belegt=#False
map(hash)\Key=Key
map(hash)\Value=Value
map(hash)\Belegt=#True
ProcedureReturn
Else
hash=(hash+1)%mapsize
EndIf
ForEver
EndProcedure
Procedure Get(Key.s)
Protected Hash.l
Hash=GetHash(Key.s)
Repeat
If map(hash)\Belegt=#False
ProcedureReturn 0
ElseIf map(hash)\key=key
ProcedureReturn map(hash)\Value
Else
hash=(hash+1)%mapsize
EndIf
ForEver
EndProcedure
For k = 0 To 9000
Add(Str(k),k)
Next
For k = 0 To 9000
Get(Str(k))
Next
Debug ElapsedMilliseconds()-start
;-----------------------------------------------------
start=ElapsedMilliseconds()
NewList test.pair()
For t = 0 To 9000
AddElement(test())
test()\value=t
test()\key=Str(t)
Next
For t = 0 To 9000
S.s=Str(t)
ForEach test()
If test()\key=S
Break
EndIf
Next
Next
Debug ElapsedMilliseconds()-start