List vs Map Benchmark

Just starting out? Need help? Post your questions and find answers here.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 763
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

List vs Map Benchmark

Post by minimy »

This is a simple benchmark to know where and when is better use Map or List.

Code: Select all

Structure Item
  id.i
  value.i
EndStructure
NewList myList.Item()
NewMap myMap.Item()
Define startTime, endTime, i, j, searchId, foundValue
Define listTime, mapTime, listSearchTime, mapSearchTime
Define numElements = 100000
Define numSearches = 10000

; Benchmark con List
startTime = ElapsedMilliseconds()
For i = 1 To numElements
  AddElement(myList())
  myList()\id = i
  myList()\value = i * 2
Next
ForEach myList()
  i = myList()\value
Next
endTime = ElapsedMilliseconds()
listTime = endTime - startTime

; Benchmark con Map
startTime = ElapsedMilliseconds()

For i = 1 To numElements
  AddMapElement(myMap(), Str(i))
  myMap()\id = i
  myMap()\value = i * 2
Next
ForEach myMap()
  i = myMap()\value
Next
endTime = ElapsedMilliseconds()
mapTime = endTime - startTime

; Búsquedas aleatorias en List
startTime = ElapsedMilliseconds()
For j = 1 To numSearches
  searchId = Random(numElements) + 1
  ResetList(myList())
  While NextElement(myList())
    If myList()\id = searchId
      foundValue = myList()\value
      Break
    EndIf
  Wend
Next
endTime = ElapsedMilliseconds()
listSearchTime = endTime - startTime

; Búsquedas aleatorias en Map 
startTime = ElapsedMilliseconds()
For j = 1 To numSearches
  searchId = Random(numElements) + 1
  If FindMapElement(myMap(), Str(searchId))
    foundValue = myMap()\value
  EndIf
Next

endTime = ElapsedMilliseconds()
mapSearchTime = endTime - startTime
MessageRequester("Resultados Benchmark List vs Map", 
                 "Insert and tour ("+Str(numElements)+" elementos):" + #CRLF$ +
                 "  List: " + Str(listTime) + " ms" + #CRLF$ +
                 "  Map: " + Str(mapTime) + " ms" + #CRLF$ + #CRLF$ +
                 "Random search (" + Str(numSearches) + " intentos):" + #CRLF$ +
                 "  List: " + Str(listSearchTime) + " ms" + #CRLF$ +
                 "  Map: " + Str(mapSearchTime) + " ms")
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
idle
Always Here
Always Here
Posts: 6109
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: List vs Map Benchmark

Post by idle »

A map search is O(1) and degrades toward O(n) fully loaded.
A list is O(n)
That should be enough to tell you which one to use.

An issue arises if you haven't sized the map to the expected load and it's performance will degrade. PB map defaults to 512 elements. So that's not going to work well for 100k items as it causes multiple resizes and leads to multiple probes.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 763
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: List vs Map Benchmark

Post by minimy »

Thanks idle for the information. I just put this to help noobs members.
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply