Page 1 of 1

How does TableItems() work from power point of view.

Posted: Fri Jun 02, 2023 10:02 am
by Bugala
As I am wondering about how to optimize my code faster, I was starting to wonder about a solution involving TableItems() command, but main question regarding it is that does TableItems() slow down if Table is bigger.

As in:

Code: Select all

Table1 = { 1, 2 }
Table2 = {1, 2, 3, 4, 5... 100}
Now when using TableItem() to find out how many items these have, is Table1 getting result faster than table2, and if it is getting result faster, then is the difference directly 50 times slowe to get Table2?

That does it actually go through every item to see there are items, or is it able to simply check reserved memory space for Table2 to figure out how many items there are?

To give more details about the situation is that I have pathfinding that is currently checking 90 steps away at max, this is in case source is on one edge of map, and target is on other side of map.

However, I was thinking that since this situation is very unlikely, I could maybe stop checking after 70 steps already, and in case it results in no target at all, then it could simply pick one of the targets at random, since i do keep the targets in table, so I could use TableItems() to get the amount of potential targets, and then just randomly pick one of them and take a direct line towards it. This could significantly improve the speed in these rare situations and might not affect the play experience much, making it more possible to work on Amigas.

Re: How does TableItems() work from power point of view.

Posted: Fri Jun 02, 2023 11:31 am
by Flinx
You can check this with a timer. I tried it now on Windows, and to see the difference I had to put 10 Million elements in the table, so it should be no real problem on the Amiga too. But yes, it seems to need the amount of time for the number of elements.
1000000 elements: 14ms
10000000 elements: 148ms
Why not put the number of elements in a variable and use this value? You only have to adapt the value when add or remove table items.

Re: How does TableItems() work from power point of view.

Posted: Sat Jun 03, 2023 1:41 pm
by Bugala
Why not put the number of elements in a variable and use this value?
Good point, didn't realize myself. Thanks!

Re: How does TableItems() work from power point of view.

Posted: Sun Jun 04, 2023 11:38 am
by airsoftsoftwair
If you only need numeric indices, use CreateList() and the functions described in the CreateList() manual. In that case, ListItems() will return the number of items without any "counting" so it will be very fast.