Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Which method of IF statements are better in terms of optimising and efficiency? [closed]

Asked by 6 years ago

As of now, I've been using a mix of two:

method 1:

1local condition1 = true
2local condition2 = true
3local condition3= true
4 
5if condition1 and condition2 and condition3 then
6    print("all conditions are true")
7end

method 2:

01local condition1 = true
02local condition2 = true
03local condition3= true
04 
05if condition1 then
06    if condition2 then
07        if condition3 then
08            print("all conditions are true")
09        end
10    end
11end

Closed as Primarily Opinion-Based by User#19524

This question has been closed because it is a discussion about a topic focused on diverse opinions, which isn't a good fit for our Q&A format.

Why was this question closed?

2 answers

Log in to vote
1
Answered by 6 years ago

Hi Marmalados,

According to Roblox Studio, method 2 is faster. I ran multiple tests to prove that and here is the code that I used:

Time Testing Code

01method1 = 0 -- Will count how many times method 1 came out to be successful in the trials.
02method2 = 0 -- WIll count how many times method 2 came out to be successful in the trials.
03 
04for x = 1, 100000 do -- Runs 100 thousand trials on the code.
05    t1 = tick()
06 
07    if workspace:FindFirstChild("Script") and workspace:FindFirstChild("Baseplate") then
08    end
09 
10    t1 = tick() - t1
11 
12    t2 = tick()
13 
14    if workspace:FindFirstChild("Script") then
15        if workspace:FindFirstChild("Baseplate") then
View all 32 lines...

I hope I helped and had a great day/night.

Thanks,

Best regards,

KingLoneCat

0
Thank u! Marmalados 193 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

The first statement is like, if yes and yes2 and yes3 then runs the script, while method 2 is like if yes then (checkpoint) if yes2 then (checkpoint) if yes3 then (checkpoint) as in consequently checking if they are all accompanied to each other, as in if the first yes is true, then check if the 2nd yes is true, vise versa.

I believe if you want a more-effecient based script, i'd prefer method 2 as in consequently running check-ups while proceeding throughout the script. Like I said, the method 1 is just to check at first if all method's are true to proceed, while method 2 is checking if the first if statement is true, to proceed to second if statement and so on.

0
But which method would be less taxing on game performance? Marmalados 193 — 6y
1
Well, game performance really doesn't determine on a script's layout but I believe what you're wondering is what's the best way to not request to much information from the game, which you're not really requesting much here unless you're planning to use RemoteEvents, as in I would say method 1 would be best for that, as it runs it through the first line. The method 2 would just check continuously. Resplendid 3 — 6y