Embedded / nested linked lists to access objects
BlitzMax Forums/BlitzMax Programming/Embedded / nested linked lists to access objects
| ||
Hi all, I looked around on the forums here for this but couldn't find it... I can't have been the first person to test this, but so far it seems like a viable method for direct access to an item/object in a larger list. If anyone could look it over and offer input as to why it appears to be a benefit to do things this way (whether it's true or false), I'd appreciate it. Here's some BMX code I threw up to demonstrate what I had in mind. I work with several games where lots of nested iterations are needed and I always wondered if a better way to get access to a previously "picked" object could exist. I may have found it, but I'd like feedback. Basically each object in list A has an embedded list which will only contain one object chosen from a larger list. In my tests this usually gives a huge increase in speed to get back to the same object in list B. Local fvCount:Int = 0 Type thisone Field link:TLink Field target:Int Field targetlist:TList = CreateList() EndType Local a:thisone Local alist:TList = CreateList() Type thatone Field link1:TLink Field link2:TLink Field ID:Int EndType Local b:thatone Local blist:TList = CreateList() a = New thisone a.link = ListAddLast(alist,a) For n = 0 To 99999 b = New thatone b.ID = n b.link1 = ListAddLast(blist,b) Next 'old way of doing things... time:Int = MilliSecs() For a = EachIn alist For b = EachIn blist If b.ID = 99998 a.target = b.ID Exit EndIf Next Next For a = EachIn alist For b = EachIn blist If a.target = b.ID DebugLog(b.ID) Exit EndIf Next Next Print "duration for old pick loop: " + String(MilliSecs()-time) + " msecs" 'new way of doing things... time:Int = MilliSecs() For a = EachIn alist For b = EachIn blist If b.ID = 99998 b.link2 = ListAddLast(a.targetlist,b) Exit EndIf Next Next For a = EachIn alist For b = EachIn a.targetlist DebugLog(b.ID) Exit Next Next Print "duration for improved pick loop: " + String(MilliSecs()-time) + " msecs" |