|
| 13 Aug 2012 04:55 PM |
Simple questions really. Is it more efficient to use string.find() with a string or to use a for loop through a table to find out of a value is in either the table or the string. For example:
admins = {"Me","RandomScripters","Player"} for i,v in pairs(admins) do if blah == v then You get the point end end
Or,
admins = "Me,RandomScripters,Player" if string.find(admins,blah) then You get the point end
I'm guessing the strings but am not positive... |
|
|
| Report Abuse |
|
|
Tarabukka
|
  |
| Joined: 18 Jan 2011 |
| Total Posts: 394 |
|
|
| 13 Aug 2012 04:57 PM |
The for loop through a table is more efficient, but you shouldn't worry about these kind of efficiency problems, a lot of people consider it pointless, because unless you're repeating the code hundreds of thousands of times, it won't really add much.
To simplify, the for loop is more efficient because Lua stores the table sequentially in memory (I think?), but with the string.find, it has to go through and find the commas each time rather than just going straight through the list. |
|
|
| Report Abuse |
|
|
FPGA
|
  |
| Joined: 05 May 2012 |
| Total Posts: 372 |
|
|
| 13 Aug 2012 04:58 PM |
| DO NOT use string.find. What if a person joins named RandomScript? String.find will return true in that case. |
|
|
| Report Abuse |
|
|
Tarabukka
|
  |
| Joined: 18 Jan 2011 |
| Total Posts: 394 |
|
|
| 13 Aug 2012 04:59 PM |
| Actually, what I said is probably wrong in this case. Both of them are probably O(n) but one is slightly less O(n) |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2012 04:59 PM |
| I wasn't really thinking one would be so inefficient it would cause massive lag, I was just wondering because I always hear people saying, "Oh no, there's a more efficient way to do that."(That being basically anything) Well, thanks for the answer. Kind of expected to be flamed and was happily surprised. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 13 Aug 2012 05:05 PM |
Neither is very efficient, it should be done like this:
local adminMap = {somePlayer = true, someotherPlayer = true, ...} local function isAdmin(player) return adminMap[player.Name] or false end |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2012 05:05 PM |
If you require a comma, also (at beginning and end):
admins = {"Me","RandomScripters","Player"} admins = "." .. table.concat(admins,"."):lower() .. "."
if admins:find("." .. name:lower() .. ".") then ...
I'm guessing that table looping will be faster for relatively small lists (ie, <6 or so), string for lists between there and maybe like 30, and then tables faster from then on.
Of course, they're both so fast it doesn't really matter which is which, since you don't need to do this checks frequently and the list should probably never exceed a few dozen, if that. |
|
|
| Report Abuse |
|
|
Merely
|
  |
| Joined: 07 Dec 2010 |
| Total Posts: 17266 |
|
|
| 13 Aug 2012 05:11 PM |
This is what I do:
local Admins = { "Seranok", "Player", }
local function IsAdmin(player) for _, playerName in pairs(Admins) do if player.Name == playerName then return true end end return false end |
|
|
| Report Abuse |
|
|