generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Better methods than for i, v in pairs() do?

Previous Thread :: Next Thread 
Kie413Games is not online. Kie413Games
Joined: 06 May 2010
Total Posts: 3896
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
TehIcyStar is not online. TehIcyStar
Joined: 07 Mar 2010
Total Posts: 1649
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 is not online. 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 is not online. 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 is not online. 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
Wunder_Wulfe is not online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
07 Oct 2016 12:58 PM
efficiency scale:

pairs < ipairs < next < numerical
Report Abuse
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
07 Oct 2016 01:04 PM
and every "<" represents an insignificant amount of efficiency
Report Abuse
Wunder_Wulfe is not online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
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
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
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
Wunder_Wulfe is not online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
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
CodeBreadpudding is not online. CodeBreadpudding
Joined: 24 Aug 2016
Total Posts: 1286
07 Oct 2016 01:17 PM
local t = {}
for p = 1, #t do
-- Code
end

Might be faster than pairs.
Report Abuse
Wunder_Wulfe is not online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
07 Oct 2016 01:19 PM
why is it that people like to repeat what was already noted on the forum clearly
Report Abuse
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
07 Oct 2016 01:19 PM
um wunder just said that but OK
Report Abuse
Kie413Games is not online. Kie413Games
Joined: 06 May 2010
Total Posts: 3896
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
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
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
Wunder_Wulfe is not online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
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
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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 is not online. 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
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
07 Oct 2016 02:08 PM
You're right about most things, but ipairs is significantly slower than a numerical for loop
Report Abuse
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
07 Oct 2016 02:09 PM
it's significantly insignificantly slower than a numerical for loop*
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
07 Oct 2016 02:10 PM
yeah.
Report Abuse
Wunder_Wulfe is not online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
07 Oct 2016 02:15 PM
pairs == next < ipairs < numerical

better?

but doesnt pairs just return next?
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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
Wunder_Wulfe is not online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image