[07 Apr 2008] Speeding up access to items in lists

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
User avatar
TheMartian
Posts: 109
Joined: Sun Feb 28, 2010 12:51 pm

[07 Apr 2008] Speeding up access to items in lists

Post by TheMartian »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 07 Apr 2008 18:42:29 -0000

Hi

I guess this is in the 'tips and tricks' category.

While working with some nested lists I decided to see if there were ways of speeding up access to individual items in the lists and to shorten the work involved in writing code accessing these items.

It turns out there are very real improvements to be found in both areas. First of all you have to realize, that if you have a list called MyList and assigns another variable like this:

x = MyList

...then x becormes in effect an alias for the same list. Both names points to the same list, so updating one also updates the other because they are one and the same. Interestingly one can be a local variable while the other is global, and local varables ARE faster to work with.

The second thing is that if you assign a variable (preferably local) to a sublist, you can shorten the code considerably. What would you rather write....

MyMainList[0][0].entry1 or z.entry

...when you work with the item?

Finally you can assign a variable directly to the item in the list as in n=MyList[0][0].entry1. In that case we have two distinct variables. This may be useful if you have a lot of operations to perform on the specific entry. Do it on a local copy and assign it back to the entry in the list afterwards.

This last case is perhaps somewhat special. But for the others, and particularly the one with local z=MyMainList[0][0], the benefits are clear both speedwise and in terms of the size of your code. I now use the trick extensively inside functions when many items in a list needs to be updated - mainly because it saves some typing - but the speed gain is there too.

Below is a code example that runs the same type of code several times, each time improving a bit on the original code. On my A1 (933 MHz) the results are as follows:

5704 mS 5672 mS 5439 mS 4515 mS 4242 mS

That is a 25% speed increase in the last case and about 20% on the more likely scenario next to last. Who says nothing is free...

Have fun

Jesper

Code: Select all

@DISPLAY {color = #SILVER}

MyMainList = {}
MyMainList[0] = {}
MyMainList[0][0] = {}
MyMainList[0][0].entry1 = 42

local i

StartTimer(1)
for i=1 to 1000000
	MyMainList[0][0].entry1 = MyMainList[0][0].entry1+1
next
nprint("No tricks used  : ",GetTimer(1))
StopTimer(1)

x=MyMainList
StartTimer(1)
for i=1 to 1000000
	x[0][0].entry1 = x[0][0].entry1+1
next
nprint("short list name : ",GetTimer(1))
StopTimer(1)

local y=MyMainList
StartTimer(1)
for i=1 to 1000000
	y[0][0].entry1 = y[0][0].entry1+1
next
nprint("local list name : ",GetTimer(1))
StopTimer(1)

local z=MyMainList[0][0]
StartTimer(1)
for i=1 to 1000000
	z.entry1 = z.entry1+1
next
nprint("point to sublist: ",GetTimer(1))
StopTimer(1)

local n=MyMainList[0][0].entry1
StartTimer(1)
for i=1 to 1000000
	n = n+1
next
MyMainList[0][0].entry1=n
nprint("set as variable : ",GetTimer(1))
StopTimer(1)

waitleftmouse
end
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

[07 Apr 2008] Re: Speeding up access to items in lists

Post by Allanon »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 07 Apr 2008 22:47:51 -0000

Very useful tricks, thank you! 25% of speed increase is a huge boost in time-crit applications!
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
Locked