SpeedTesting table creation Table[n] = x, CreateList, and Copytable comparison.

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1399
Joined: Sun Feb 14, 2010 7:11 pm

SpeedTesting table creation Table[n] = x, CreateList, and Copytable comparison.

Post by Bugala »

I found the result surprising, I was expecting CopyTable to be the fastest way to set a new table in situation where table has some predefined values, but seems creating them from new is the fastest way to go:

Code: Select all

HowManyTimes = 10000
HowManyIndexes = 10000

StartTimer(1)
For n = 1 To HowManyTimes 
	Testi = CreateList()
	For n = 1 To HowManyIndexes
		InsertItem(Testi, {distance=200})
	Next
Next
CreateList = GetTimer(1)


StartTimer(2)
For n = 1 To HowManyTimes 
	testi = {}
	For n = 1 To HowManyIndexes
		testi[n] = {distance=200}
	Next
Next
RegularTable = GetTimer(2)

StartTimer(3)
For n = 1 To HowManyTimes
	Testi = CopyTable(Testi)
Next
CopyTbl = GetTimer(3)

DebugPrint("CreateList:"..CreateList.." RegularTable:"..RegularTable.." CopyTable:"..CopyTbl)
Result:
CreateList: 6
RegularTable: 3
CopyTable: 18648

Tested on Hollywood 11, Windows 11.


This also brings a question, as CreateList lists are supposed to be fastest, and I have noticed in other of my tests too, that often using Table[n] = x, is faster than using CreateList possibilities, is this because I am using windows, or is this true on Amiga too? For I am especially trying to optimize my code to work faster on AOS4 machines, hence this info would be valuable, especially since for example in this examples case, using Talbe[n] = x to create the table, was double the speed compared to using CreateList with InsertItem.

And especially surprising was how much slower CopyTable option was, which is completely in different league.
User avatar
jPV
Posts: 739
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: SpeedTesting table creation Table[n] = x, CreateList, and Copytable comparison.

Post by jPV »

There are few things in the code that may explain something. Haven't tested properly myself yet...

1) You are using the same global variable for the For loops, which makes two first tests to run only once, but the last one (CopyTable) for 10000 times. This most likely explains the CopyTable result. Fix it by using different variable names for inner loops or use local variables (For Local n = 1...)

2) In the first test you use InsertItem to insert items to the end of the list, not to a fixed index, maybe it would be more fair to do the same for the second test too instead of putting it in a pre-defined index.

3) There might be more penalty to init a table by CreateList than with {}, maybe just try to init table once and compare the speed then?
Bugala
Posts: 1399
Joined: Sun Feb 14, 2010 7:11 pm

Re: SpeedTesting table creation Table[n] = x, CreateList, and Copytable comparison.

Post by Bugala »

You are right, I completely missed I was using same n on both loops inside those first two, no wonder they were so much faster.

By changing them into locals, as I should have done right from the beginning, it took so long that I changed the numbers to 1000 instead of 10000:

CreateList: 416
RegularTable: 302
CopyTable: 215

Now the CopyTable is fastest, like I expected it to be.


Regarding the InsertItem and fixed index. As far as I have understood, when using CreateList, you are not supposed to put stuff using direct indexes, but you should use InsertItem: "The disadvantage is that adding or removing items may only be done via InsertItem() and RemoveItem(). You must not add or remove items from optimized lists by modifying the table directly. It's necessary to use the functions mentioned above. "

For this reason I made the test using direct indexes for the regular table only.

But using direct indexes, either directly, as in table[n] = x, or through insertitem, as in InsertItem(table, x, n), were close to same speed as regular table, but regular table was still slightly faster. in way of, Createlist = 300, regular table = 290, so not that much difference anymore.

Regarding the penalty of creating list, you might be right on this one. Once again, not that big difference, but there does seem to be a difference when putting "HowManyTimes" into only 1, and then increasing the number of HowManyIndexes only to make it go multiple times.

Difference is still big, but not as big, in my example:
398 vs 338, making the difference be around 20 percent, while in the original example difference was 30 percent.
User avatar
jPV
Posts: 739
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: SpeedTesting table creation Table[n] = x, CreateList, and Copytable comparison.

Post by jPV »

I guess it depends of the use case. With direct indexes there may not be difference, but if you're adding stuff to the end of a table, then CreateList is always much faster.

Try changing the regular table to "InsertItem(Testi, {distance=200})" or "testi[ListItems(testi)] = {distance=200}" or something like that to get the item inserted as the last. Then there's huge difference.
Bugala
Posts: 1399
Joined: Sun Feb 14, 2010 7:11 pm

Re: SpeedTesting table creation Table[n] = x, CreateList, and Copytable comparison.

Post by Bugala »

Thanks, Took a try of using InsertItem and now there was a big difference, not that far from 10 times faster being using CreateList.

Finally that makes sense. I always been wondering about how come CreateList is supposed to be 10 times faster, when it often times in my tests have even lost to regular table, but so that is the point, that the speed benefit comes when using InsertItem, RemobeItem, ListItems, without giving direct indexes, and then using direct indexes, it might be that regular table actually might beat the CreateList versions.

Good, now I can use this knowledge to decide which one is better to use when needing to optimize the speed.
Post Reply