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: Looping through objects - why would you use numerical fors?

Previous Thread :: Next Thread 
G0odby3 is not online. G0odby3
Joined: 26 Jan 2016
Total Posts: 5587
07 Aug 2016 12:13 PM
you wouldn't. that's ugly and inefficient.


- AbstractMadness, currently terminated. 0/1.5M+ :(
Report Abuse
DreamsWork is not online. DreamsWork
Joined: 20 Jan 2010
Total Posts: 36796
07 Aug 2016 12:15 PM
remember that time u lost rstf


Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
07 Aug 2016 12:15 PM
Too bad you're wrong :/
Report Abuse
G0odby3 is not online. G0odby3
Joined: 26 Jan 2016
Total Posts: 5587
07 Aug 2016 12:18 PM
local nstart = tick()

for i,v in next, workspace:GetChildren() do
print(v.Name)
end

local nfinish = tick() - nstart

local start = tick()

for i = 1,#workspace:GetChildren() do
print(workspace:GetChildren()[i].Name)
end

local finish = tick() - start

print("It took ".. finish .." seconds to loop using numberical for.")

print(nfinish > finish and "Next is faster by " .. nfinish - finish .. " seconds." or "Numerical is faster by " .. finish - nfinish .. " seconds.")

too bad I'm right ://////


- AbstractMadness, currently terminated. 0/1.5M+ :(
Report Abuse
Color3new is not online. Color3new
Joined: 05 Jul 2009
Total Posts: 64
07 Aug 2016 12:18 PM
It's ugly but actually more efficient.
Report Abuse
G0odby3 is not online. G0odby3
Joined: 26 Jan 2016
Total Posts: 5587
07 Aug 2016 12:19 PM
Next is faster by 0.00071191787719727 seconds.


- AbstractMadness, currently terminated. 0/1.5M+ :(
Report Abuse
Color3new is not online. Color3new
Joined: 05 Jul 2009
Total Posts: 64
07 Aug 2016 12:19 PM
I believe Anaminus has a blog of some sorts where he does a similar test, he finds that numerical for loops are indeed quicker and more efficient.
Report Abuse
G0odby3 is not online. G0odby3
Joined: 26 Jan 2016
Total Posts: 5587
07 Aug 2016 12:19 PM
lol typo in my script... numerical*


- AbstractMadness, currently terminated. 0/1.5M+ :(
Report Abuse
G0odby3 is not online. G0odby3
Joined: 26 Jan 2016
Total Posts: 5587
07 Aug 2016 12:21 PM
@Color3

run that code.

and if you remove the prints, it's even faster!


- AbstractMadness, currently terminated. 0/1.5M+ :(
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
07 Aug 2016 12:21 PM
i agree

for i,v in next, thing do

end

much more cleaner

the only time i would use numerical is looping through a string in then using
string:sub(i,i) to get the character


Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
07 Aug 2016 12:21 PM
Negligible is a generous term for the difference.
'Next is faster by 5.1975250244141e-005 seconds.'
Report Abuse
G0odby3 is not online. G0odby3
Joined: 26 Jan 2016
Total Posts: 5587
07 Aug 2016 12:21 PM
cleaner AND faster! :D


- AbstractMadness, currently terminated. 0/1.5M+ :(
Report Abuse
Color3new is not online. Color3new
Joined: 05 Jul 2009
Total Posts: 64
07 Aug 2016 12:21 PM
You shouldn't be using next, either, if you're concerned about speed.
Use pairs.
Report Abuse
Color3new is not online. Color3new
Joined: 05 Jul 2009
Total Posts: 64
07 Aug 2016 12:22 PM
It was crazyman32, my bad.

I would post you the link, but I'd probably get cd'ed.
Report Abuse
Color3new is not online. Color3new
Joined: 05 Jul 2009
Total Posts: 64
07 Aug 2016 12:23 PM
"
So to those who say “next” is so much faster: Let me introduce you to the numeric for loop. Oh yes, the good ol’ numeric for loop. Many have abandoned this method as it doesn’t look as pretty as generic for loops. While it might not look as clean, it does come with some performance boosts. Let’s run the same tests as above, but with the numeric for loop:
local start = os.clock() -- Start time
for iteration = 1,100 do -- Iterate 100 times
for i = 1,#data do local v = data[i] end
end
print(os.clock() - start) -- Print duration
RESULT:
> 2 seconds (a few tests seemed to be within 1.9 and 2.2 seconds)
Wow, 2 seconds! That’s right, 2 seconds! What an improvement! That is more than double the speed of the first two methods shown.
Some Quick Conclusions
If you are using “next” as an optimization method, stop! Instead, use a numeric for loop! If you’re using “next” as an optimization method but don’t really need to optimize, use “pairs” instead, unless you just prefer the “next” method. If you simply perfer using “next,” keep rocking it.
"
Report Abuse
G0odby3 is not online. G0odby3
Joined: 26 Jan 2016
Total Posts: 5587
07 Aug 2016 12:23 PM
@Color3

nope!

local nstart = tick()

for i,v in next, workspace:GetChildren() do
print(v.Name)
end

local nfinish = tick() - nstart

local start = tick()

for i,v in pairs(workspace:GetChildren()) do
print(v.Name)
end

local finish = tick() - start

print(nfinish > finish and "Next is faster by " .. nfinish - finish .. " seconds." or "Pairs is faster by " .. finish - nfinish .. " seconds.")

> Next is faster by X seconds.


- AbstractMadness, currently terminated. 0/1.5M+ :(
Report Abuse
Color3new is not online. Color3new
Joined: 05 Jul 2009
Total Posts: 64
07 Aug 2016 12:26 PM
"Pairs is faster by 0.0020017623901367 seconds."

Just ran your code and got this.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
07 Aug 2016 12:28 PM
"You shouldn't be using next, either, if you're concerned about speed."
Wrong. They are literally the same. In fact, next is faster but not really noticeable because it doesn't increase with the size of the loop, so it won't matter.

Numerical loops are faster at iterating over lists. Not to mention they are easier to understand.

"Next is faster by"
No, you tested totally wrong. It's too biased. You call GetChildren every single iteration, where as you only do it once with next.


Report Abuse
Toonshield is online. Toonshield
Joined: 04 Mar 2015
Total Posts: 518
07 Aug 2016 12:31 PM
I got: Next is faster by 0.00017333030700684 seconds
Report Abuse
Color3new is not online. Color3new
Joined: 05 Jul 2009
Total Posts: 64
07 Aug 2016 12:32 PM
@Jarod;

I was taking my facts from Crazyman32's blog, whose scripting abilities I trust a lot more than anyone else in this thread.
Report Abuse
Wowgnomes is not online. Wowgnomes
Joined: 27 Sep 2009
Total Posts: 26255
07 Aug 2016 12:34 PM
hi u replied to me once on your tweeter and called me mean words after i paraphrased you

not a very nice person
Report Abuse
Color3new is not online. Color3new
Joined: 05 Jul 2009
Total Posts: 64
07 Aug 2016 12:34 PM
"However, many people do this as an optimization method. In fact, I’ve had people insult me for using “pairs” instead of “next” because they say it is so much faster. Whether you’re an advocate for using “next” or you’re just interested in what the heck I’m ranting about, let’s do some speed tests to test these theories."

And then he goes on to explain why it's better. Can't really copy it all easily because of the code boxes. Again, I'd post the link but it's off site.
Report Abuse
G0odby3 is not online. G0odby3
Joined: 26 Jan 2016
Total Posts: 5587
07 Aug 2016 12:35 PM
just post the link on Twitter, and then link us to the Twitter post


- AbstractMadness, currently terminated. 0/1.5M+ :(
Report Abuse
Color3new is not online. Color3new
Joined: 05 Jul 2009
Total Posts: 64
07 Aug 2016 12:39 PM
https://twitter.com/Scriptos_/status/762342283786096642
Report Abuse
G0odby3 is not online. G0odby3
Joined: 26 Jan 2016
Total Posts: 5587
07 Aug 2016 12:40 PM
@Scriptos_'s Tweets are protected.

Only confirmed followers have access to @Scriptos_'s Tweets and complete profile. Click the "Follow" button to send a follow request.


- AbstractMadness, currently terminated. 0/1.5M+ :(
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