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: Efficiency

Previous Thread :: Next Thread 
StealthKing95 is online. StealthKing95
Joined: 13 Dec 2008
Total Posts: 4263
21 Aug 2013 10:39 AM
Me and a friend kinda had a competition, who wrote a thing more efficiently.

Which one do you think is "better" in terms of efficiency?

--MY ATTEMPT:
function weigh(sub,prelist)
local sub = sub:children()
local total = prelist
for i = 1, #sub do
if sub[i]:IsA("BasePart") then
total = total + sub[i]:GetMass()
else
weigh(sub[i],total)
end
end
return total
end
print("Total Mass: "..weigh(workspace.Model,0))


--MY FRIENDS ATTEMPT:


local dir = script.Parent
local tmass = 0

function search(mdl)
local f = mdl:children()
for i = 1, #f do
if f[i]:IsA("BasePart") then
tmass = tmass + f[i]:GetMass()
end
end
end

search(dir.Body)
search(dir.Vital)
search(dir.Vital.Gear)

print(tmass)
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
21 Aug 2013 10:43 AM
I think both are inefficient
Report Abuse
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
21 Aug 2013 10:58 AM
function weigh(n)
local z = n:GetChildren();
if z == 0 then
return (n:IsA("BasePart") and n:GetMass()) or 0;
end
local weight = 0;
for i = 1, #z do
weight = weight + weigh(z[i]);
end
return weight;
end
weigh(model);

That works, right?
I really don't think any of these is significantly different in speed, but some of them are better written....
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
21 Aug 2013 12:00 PM
Your example does not even work for models with nested children. You just throw away the results from the weigh() calls on the children, and the "prelist" argument is useless.
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 12:41 PM
local isA = workspace.IsA
local children = workspace.GetChildren
local getMass = Instance.new'Part'.GetMass

local getModelMass

getModelMass = function(model, mass)
local mass = mass or 0
for _, v in next, children(model) do
if isA(v, 'BasePart') then
mass = mass + getMass(v)
elseif isA(v, 'Model') then
mass = getModelMass(v, mass)
end
end
return mass
end

local workspaceMass = getModelMass(workspace)
print(workspaceMass)

What do you think of that, blocco?
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
21 Aug 2013 12:43 PM
[ Content Deleted ]
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 12:45 PM
Why not? D:
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
21 Aug 2013 12:46 PM
[ Content Deleted ]
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 12:47 PM
I remember an old post where you said it WAS worth it. Why must everything I be told be contradicted..?
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
21 Aug 2013 12:49 PM
@Notunknown99

The mass argument has no purpose in your code. It's entirely unnecessary.

Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 12:50 PM
Yeah, I just noticed that AFTER I posted...
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
21 Aug 2013 12:59 PM
[ Content Deleted ]
Report Abuse
Tenal is not online. Tenal
Joined: 15 May 2011
Total Posts: 18684
21 Aug 2013 01:00 PM
:children()
Report Abuse
MrgamesNwatch is not online. MrgamesNwatch
Joined: 02 Feb 2009
Total Posts: 7729
21 Aug 2013 01:09 PM
yeh

function modelMass(model)
        local t = 0
        for i,v in pairs(model:GetChildren()) do
                if v:IsA("BasePart") then
                        t = t + v:GetMass()
                end
                if #v:GetChildren() > 0 then
                        t = t + modelMass(v)
                end
        end
        return t
end



~ :children() :children() :children() :children() :children() :children() :children() :children() :children() :children()
Report Abuse
RGBI is not online. RGBI
Joined: 12 Aug 2013
Total Posts: 47
21 Aug 2013 02:18 PM
parts=0;

getModelMass=function(MODEL)
for index,rr in pairs(MODEL:children())do
if rr:isA'BasePart'then
parts=parts+1;
end;
end;
end;

To me it looked like we were counting parts to get the mass and if we weren't then my bad.
Report Abuse
ThePC8110 is not online. ThePC8110
Joined: 04 Jun 2011
Total Posts: 486
21 Aug 2013 03:30 PM
function Weigh(Object)
local Final = 0
for k,v in next,Object:children() do
Final = Final + (v:IsA'BasePart' and v:GetMass() or 0)
Final = Final + Weigh(v)
end
return Final
end

Right?
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 03:49 PM
getModelMass = function(model)
local mass = 0
for i, v in next, model:GetChildren() do
if v:IsA'BasePart' then
mass = mass + v:GetMass()
elseif v:IsA'Model' then
mass = mass + getModelMass(v)
end
end
return mass
end

print(getModelMass(workspace))

--HAPPY?
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 03:59 PM
Now I need to go and find the most efficient method I can... Looking at next v ipairs v numeric fors :P
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 04:06 PM
I found this one to be best: (It ran the code until starttime+60<=tick(), then printed i/(tick()-starttime) to get the number of runs per second) I found that a numeric for loop gave best results:

getModelMass = function(model)
local mass = 0
for i = 1, #model do
if model[i]:IsA'BasePart' then
mass = mass + model[i]:GetMass()
elseif model[i]:IsA'Model' then
mass = mass + getModelMass(model[i]:GetChildren())
end
end
return mass
end
print(getModelMass(workspace:GetChildren()))
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
21 Aug 2013 04:14 PM
[ Content Deleted ]
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
21 Aug 2013 04:15 PM
[ Content Deleted ]
Report Abuse
SN0X is not online. SN0X
Joined: 24 Oct 2011
Total Posts: 7277
21 Aug 2013 04:25 PM
What do you guys think of mine? It doesn't look good, obviously, but I optimized it a lot and that's why it's not pretty. I'd be shocked if any of you could beat me.

MODEL_WEIGHT=0
MODEL_PARTCOUNT = 0
MODEL = nil
MODEL2 =nil
CURRENTPART = nil
CURRENTPARTWEIGHT = 0

FIND_MODEL_PARTS = function()
MODEL_PARTCOUNT=MODEL_PARTCOUNT+1
end

GET_WEIGHT = function()
if CURRENTPART ~= nil then
if CURRENTPART.Parent == game:findFirstChild("Workspace") and CURRENTPART.CURRENTPART.ClassName == "Part" or CURRENTPART.ClassName == "TrussPart" or CURRENTPART.ClassName == "Wedge" or CURRENTPART.ClassName == "CornerWedge" then
CURRENTPARTWEIGHT = CURRENTPART:GetMass()
end
else
table.foreach(CURRENTPART:children(), FIND_MODEL_PARTS)
if MODEL_PARTCOUNT > 0 then
print'ERROR CANNOT DO SUB-MODELS SORRY IT WOULD BE INEFFICENT'
end
table.foreach(MODEL:children(), FIND_MODEL_PARTS)
end
MODEL_WEIGHT = MODEL_WEIGHT + CURRENTPARTWEIGHT
end

FIND_MODEL_WEIGHTS = function()
table.foreach(MODEL:children(), FIND_MODEL_PARTS)
for CHILD_NUM = 1, MODEL_PARTCOUNT do
CURRENTPART = MODEL:children()[CHILD_NUM]
GET_WEIGHT()
end
end

MODEL = script.Parent -- YOUR MODEL HERE OR ERROR WARNING BASIC Lua REQ.

FIND_MODEL_WEIGHTS()

print(MODEL_WEIGHT)
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 04:29 PM
"CURRENTPART.CURRENTPART.ClassName == "Part"" after "CURRENTPART.Parent == game:findFirstChild("Workspace")"?
Report Abuse
SN0X is not online. SN0X
Joined: 24 Oct 2011
Total Posts: 7277
21 Aug 2013 04:32 PM
^

huh?

so??
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 04:33 PM
Well whats with the CURRENTPART.CURRENTPART? It doesn't appear elsewhere.
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