Linked List Vs Array

BlitzMax Forums/BlitzMax Programming/Linked List Vs Array

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

@Leadwerks

If you want to speed up you TList access dont use foreach manually walk the list. Foreach is nice but MAX implmentation of enumerators is not real fast, way too much overhead in function calls. So if you have to walk a list many times best optimization you can do is inline it using the raw links. Granted if the underlying implmentation changes your code will break, so you might consider implmenting your own version.



SuperStrict

Type TItem
	Field Name : String
	Function Create:TItem(I:Int)
		Local r : TItem = New TItem
		r.Name = "Item "+I
		Return r
	End Function
End Type


Const ITEMCOUNT:Int = 1000

Global ItemList : TList =  New TList

For Local i:Int = 0 Until ITEMCOUNT
 ItemList.Addlast(TItem.Create(i))
Next

Local starttime:Int= MilliSecs()
Local c:Int = 0
For Local t:Int = 0 Until 1000
	For Local item :TItem= EachIn ItemList 
		c:+1
	Next
Next	
Print (MilliSecs()-starttime)+"ms"

starttime:Int= MilliSecs()
c:Int = 0
For Local t:Int = 0 Until 1000
	Local link:TLink= ItemList.FirstLink()
	While link
	  Local item:TItem=TItem(link._value)
	  If link._succ._value<>link._succ 
		link=link._succ
	  Else
	    link=Null
	  End If		
	  C:+1
	Wend
Next	
Print (MilliSecs()-starttime)+"ms"

35ms
12ms
[/code]
Results from my machine for this simple little list thats close to 300% faster naviagation of the list.

Hope this helps
Doug Stastny