|
| 07 Oct 2016 11:56 AM |
I seem to be finding myself using for i, v in pairs() do a lot. Whenever I want to get a bunch of objects from a group of objects this is really useful.
However is there any way to use this properly and more efficiently? Also is there other methods like this? |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 12:01 PM |
| imo, in pairs() is efficient and neat, there is no reason on why you should use anything else. |
|
|
| Report Abuse |
|
|
wfvj014
|
  |
| Joined: 30 Apr 2012 |
| Total Posts: 145 |
|
|
| 07 Oct 2016 12:01 PM |
using;
for i,v in next, table do
is slightly more efficient due to how ipairs actuals works. You really wont notice anything tho, and the effects are very miniscule and trivial. |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 07 Oct 2016 12:01 PM |
Flux could tell you about the efficiency and intentions behind the different types of iterative processes, the only few I'm aware of are listed here. I'm not sure about the difference between pairs, ipairs, and in next, I've never looked too deeply. "in pairs" has historically served me very well ^_^
http://wiki.roblox.com/index.php?title=Generic_for#Standard_Library_Iterators
There is also an example separated from those few on the page that covers another iteration strategy scripters often employ, for i = 1,100 do print(i) end will print numbers from 1 to 100 ^_^
|
|
|
| Report Abuse |
|
|
KreoBox
|
  |
| Joined: 24 Aug 2016 |
| Total Posts: 356 |
|
|
| 07 Oct 2016 12:26 PM |
| When looping over a table of children prefer ipairs to pairs. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 12:58 PM |
efficiency scale:
pairs < ipairs < next < numerical |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:04 PM |
| and every "<" represents an insignificant amount of efficiency |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:06 PM |
| < represents that that method is less efficient than the former which is why i used 'less than' so yes that is the physical order but for iterating over a dictionary you cannot use a regular numeric loop |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:08 PM |
im not stupid obviously < means less than
i was just referring to the fact that the difference is so insignificant it really doesn't matter and micro efficiency is a waste of time and a bad idea especially if it sacrifices cleaner and more legible code |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:10 PM |
| yea the only inefficiency you need to worry about on roblox is actual total calculation or processing time and things that seem laggy when you run them because a fraction of a millisecond really won't impact your code as much |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:17 PM |
local t = {} for p = 1, #t do -- Code end
Might be faster than pairs. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:19 PM |
| why is it that people like to repeat what was already noted on the forum clearly |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:19 PM |
| um wunder just said that but OK |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:44 PM |
Thanks to everyone for helping. So it's not uncommon to be using pairs a lot in scripts? |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:46 PM |
| it depends on the script obviously but there's nothing wrong with it as long as it's not lagging the server or the client |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 01:46 PM |
if you use it over 30 times per second you should be using next
for key,value in next,table do print(key,value) end
unless you have an array and not a dictionary
for key=1,#table do local value = table[key] print(key,value) end |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 02:00 PM |
No. That's barely going to make a difference. That's called "stupid optimization."
OP using pairs for all tables is perfectly fine. There is no "better" method there are only "different" methods.
Pairs/next iterate over the entire table ipairs iterates over only the array part (but does not go over holes)
When you care about speed and simplicity and you're dealing with an array: use a numerical for loop If you're dealing with a dictionary, use pairs OR next. |
|
|
| Report Abuse |
|
|
KreoBox
|
  |
| Joined: 24 Aug 2016 |
| Total Posts: 356 |
|
|
| 07 Oct 2016 02:05 PM |
"efficiency scale:
pairs < ipairs < next < numerical"
Would you look at that, talking nonsense.
Pairs is a utility function, it just returns next and the table. Ipairs is similar to pairs, but the function it returns isn't exposed in the environment.
Ipairs is much faster than pairs/next. Around as fast as a pure numeric loop. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 02:08 PM |
| You're right about most things, but ipairs is significantly slower than a numerical for loop |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 02:09 PM |
| it's significantly insignificantly slower than a numerical for loop* |
|
|
| Report Abuse |
|
|
| |
|
|
| 07 Oct 2016 02:15 PM |
pairs == next < ipairs < numerical
better?
but doesnt pairs just return next? |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 02:17 PM |
Yes, all pairs does is return next, the table, and nil
You can't say "pairs is slower than ipairs" because they are both DIFFERENT. It's like saying "print is slower than Instance.new" It doesn't make any sense to compare them because ipairs can't even go over the non-array part |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2016 02:19 PM |
| i personally only use next and when making iterators i use numerical because it takes less calculation time |
|
|
| Report Abuse |
|
|