Hash-Map vs LL

Share your advanced PureBasic knowledge/code with the community.
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Hash-Map vs LL

Post by Nik »

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
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

Nik, You say
One Debug says more then 1000 words
I say
One comment line says more then 1000 Debugs
Anthony Jordan
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

akj wrote:I say
One comment line says more then 1000 Debugs
I second this!
I like logic, hence I dislike humans but love computers.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

with debugger: first is 112 times faster
without debug: first is 38 times faster
Never speed test with debugger.
I like the simple example though, and the speed is STILL improved a lot!
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post by Konne »

Here is your explanation:
http://en.wikipedia.org/wiki/Hash_table

I think the code is so trivial (and the procedure and Variable names are so good) that it should be easy to understand even without comments.

Awesome example thx :D
One comment line says more then 1000 Debugs
Oh and thx to Niks C++ style typing there are many comment lines.
Apart from that Mrs Lincoln, how was the show?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

... but what is it supposed to do? :|
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post by Konne »

Storing data efficiently in tasks of searches.
Apart from that Mrs Lincoln, how was the show?
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

...but the comparison is like "apples vs. pears"...
These data-structures do have totally different qualities, as well as arrays, trees, stacks or heaps do have their individual pro and contra.

...anyway: Nice small hashmap-example. :)
%1>>1+1*1/1-1!1|1&1<<$1=1
Post Reply