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: Which is more efficient

Previous Thread :: Next Thread 
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Sep 2015 07:45 AM
Out of total curiosity, which of these code snippets are more efficient?

#1, average recursion:

function RenameParts(par, name)
for _, child in pairs(par:GetChildren()) do
if child.ClassName == "Part" then
child.Name = name
end
RenameParts(child, name)
end
end



#2, whatever you call this:

function RenameParts(children, name, i)
local child = children[i]
if child then
if child.ClassName == "Part" then
child.Name = name
end
RenameParts(children, name, i+1)
return RenameParts(child:GetChildren(), name, 1)
end
end
RenameParts(workspace:GetChildren(), "YOLO", 1)





They essentially accomplish the same task, but which does it better? And how would you label the second one?
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Sep 2015 07:46 AM
Note; I didn't call the function in the 1st example, I only called it in the 2nd to show how the 3rd argument should be 1.

I could've made the 3rd argument optional but I preferred not add the extra calculation.
Report Abuse
TheCurrencyExchange is not online. TheCurrencyExchange
Joined: 09 Dec 2012
Total Posts: 6704
27 Sep 2015 07:59 AM
test them
#code
function testEff(funct)
t=tick()
funct()
return tick()-t
end
Report Abuse
powerhotmail123 is not online. powerhotmail123
Joined: 11 Apr 2011
Total Posts: 5041
27 Sep 2015 08:00 AM
They're virtually the exact same thing. I ran some tests.

The second one was faster than the first one. In the first test.
In the second test, the first one was faster.

Maybe, if you use a numerical for for the 1st function, then it would be faster.

Enjoying your stay at the Scripters Forum? Join this! http://www.roblox.com/My/Groups.aspx?gid=2582784
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Sep 2015 08:11 AM
function RenameParts(children, name)
for i = 1, #children do
local child = children[i]
if child.ClassName == "Part" then
child.Name = name
end
RenameParts(child:GetChildren(), name)
end
end

RenameParts(workspace:GetChildren(), "YOLO")



Try that one @power
Report Abuse
AdvancedDrone is online. AdvancedDrone
Joined: 17 Apr 2010
Total Posts: 2599
27 Sep 2015 08:12 AM
war pls help me

http://www.roblox.com/Forum/ShowPost.aspx?PostID=175038789



chekm8
Report Abuse
powerhotmail123 is not online. powerhotmail123
Joined: 11 Apr 2011
Total Posts: 5041
27 Sep 2015 08:28 AM
It's actually slower. I modified it a bit, and the result made it the fastest.

Here's the code:

--1
function RenameParts(children, name)
for i = 1, #children do
local child = children[i]
if child.ClassName == "Part" then
child.Name = name
end
RenameParts(child:GetChildren(), name)
end
end
local Start = tick()
RenameParts(workspace:GetChildren(), "YOLO")
print("Time1: "..Start - tick());

--2
function RenameParts(par, name)
for _, child in pairs(par:GetChildren()) do
if child.ClassName == "Part" then
child.Name = name
end
RenameParts(child, name)
end
end
local Start = tick()
RenameParts(workspace, "YOLO");
print("Time2: "..Start - tick());


--3
function RenameParts(children, name, i)
local child = children[i]
if child then
if child.ClassName == "Part" then
child.Name = name
end
RenameParts(children, name, i+1)
return RenameParts(child:GetChildren(), name, 1)
end
end
local Start = tick()
RenameParts(workspace:GetChildren(), "YOLO", 1)
print("Time3: "..Start - tick());

--4
function RenameParts(Parent, Name)
local Children = Parent:GetChildren();
for i = 1, #Children do
local Child = Children[i];
if Child.ClassName == "Part" then
Child.Name = Name;
end
end
end
local Start = tick()
RenameParts(workspace, "YOLO");
print("Time4: "..Start - tick());

Results:

Time1: -0.00093269348144531
Time2: -0.00057530403137207
Time3: -0.00052928924560547
Time4: -0.00052165985107422

Time1: -0.00084447860717773
Time2: -0.00066685676574707
Time3: -0.0005958080291748
Time4: -0.0001375675201416

Time1: -0.0007927417755127
Time2: -0.00072598457336426
Time3: -0.00058841705322266
Time4: -0.0001518726348877

Then I switched some of the functions. So I put the 4th function at the top, and the results were...

Time4: -0.00028157234191895
Time1: -0.0013465881347656
Time2: -0.00076174736022949
Time3: -0.00072765350341797

You can see Time4, or function 4 is the most efficient. :)

function RenameParts(Parent, Name)
local Children = Parent:GetChildren();
for i = 1, #Children do
local Child = Children[i];
if Child.ClassName == "Part" then
Child.Name = Name;
end
end
end

Enjoying your stay at the Scripters Forum? Join this! http://www.roblox.com/My/Groups.aspx?gid=2582784
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Sep 2015 08:31 AM
I can't seem to understand how the 4th is more efficient than the 1st
Report Abuse
powerhotmail123 is not online. powerhotmail123
Joined: 11 Apr 2011
Total Posts: 5041
27 Sep 2015 08:32 AM
Me too.

I was expecting scores which made Test1 and Test4 to be around the same. I'm guessing it's the local variable for :GetChildren(), rather than doing it in the arguments.


Enjoying your stay at the Scripters Forum? Join this! http://www.roblox.com/My/Groups.aspx?gid=2582784
Report Abuse
filiptibell is online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
27 Sep 2015 08:35 AM
The other functions are recursive, the 4th is not.
What's making the difference is the number of calls to Instance:GetChildren().
Report Abuse
powerhotmail123 is not online. powerhotmail123
Joined: 11 Apr 2011
Total Posts: 5041
27 Sep 2015 08:37 AM
Wait, the 4th one isn't recursive... I'm so stupid. -Facepalm-

Enjoying your stay at the Scripters Forum? Join this! http://www.roblox.com/My/Groups.aspx?gid=2582784
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
27 Sep 2015 08:37 AM
One does descendants, the other does not.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Sep 2015 08:39 AM
**facepalm**

You forgot the recursion part in #4

Thanks filip
Report Abuse
powerhotmail123 is not online. powerhotmail123
Joined: 11 Apr 2011
Total Posts: 5041
27 Sep 2015 08:40 AM
While I do tests making the 4th recursive, may I ask, why don't you put an else in the if statement to recurse. Rather than everytime. Since, parts can't have parts as children.

Enjoying your stay at the Scripters Forum? Join this! http://www.roblox.com/My/Groups.aspx?gid=2582784
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Sep 2015 08:43 AM
Yes they can, if inserted via script.
Report Abuse
powerhotmail123 is not online. powerhotmail123
Joined: 11 Apr 2011
Total Posts: 5041
27 Sep 2015 08:43 AM
Just did the tests, with #4 recursive. #4 and #1 are now basically the same. Sometimes #1 pulls ahead, and #4 pulls ahead sometimes. However, whenever #4 pulls ahead, the difference between it and #1 is greater than it is when #1 pulls ahead.

I switched them around too. Same result.

Enjoying your stay at the Scripters Forum? Join this! http://www.roblox.com/My/Groups.aspx?gid=2582784
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