I'm about to re-write a few parts of my script to optimize it, and looking around have found a few discussions about this (localize/avoid global namespace, etc).
If I have a large table (~1k entries) of the below structure: Are lookups from it going to be independent of order, alphabetical, start->finish, finish->start, etc? Just wondering if I can speed it up slightly by forcing the table to a specific order.
["a dun potion"] = " Lvl:20 vigorize critic ",
["a vial of white powder"] = " Lvl:15 blindness - poison - haste ",
["a vial of dolomite"] = " Lvl:50 stone - dispel invisible - dispel good ",
Optimization came up because I have a large sql database that I just added a function to access it. Individual selects are fast - If it has to do a thousand of them though, it lags mudlet a couple seconds - Noticable. If I keep a cache of a subset of the database (the most referenced results), and consult that cache prior to running a select against the database, I have a noticeable speed increase: From lagged a couple seconds, to realtime.
Optimize script?
Re: Optimize script?
That sort of table is not ordered in any particular fashion. If you iterate it a thousand times, it could potentially iterated a thousand different orders.
Or it might iterate in the same order every time. You just can't depend on it.
Or it might iterate in the same order every time. You just can't depend on it.
-
- Posts: 334
- Joined: Thu Mar 05, 2015 2:53 am
Re: Optimize script?
It's more a lua question I guess then a mudlet question, however:
Not iterating, but individual lookup:
If values for table x exist for ["a"], ["b"], ["c"], ["d"], ["e"] and I want to know the value of x["b"].
Does lua find it by an index, iterate through from the start of the table looking for a match, or end of the table? It couldn't be random, there would have to be some sort of order. I would think it should be possible for me to populate a table in a specific order to have higher probability results checked first.
Not iterating, but individual lookup:
If values for table x exist for ["a"], ["b"], ["c"], ["d"], ["e"] and I want to know the value of x["b"].
Does lua find it by an index, iterate through from the start of the table looking for a match, or end of the table? It couldn't be random, there would have to be some sort of order. I would think it should be possible for me to populate a table in a specific order to have higher probability results checked first.
Re: Optimize script?
when you look up x["b"] then it finds it by index. It's a hashed index, so it should go directly to the place in memory the value for that hash is stored, iirc.