Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Help 

[ActionScript] What is faster? Multiple IF statements or OR?

Which is faster?

Code
if (something1 == true) {
return;
}
if (something2 == true) {
return;
}
if (something3 == true) {
return;
}

OR

Code
if ((something1 == true) || (something2 == true) || (something3 == true)) {
return;
}
Send me $1 in BTC or tomorrow you'll die of head lice.
1GHy4B8QJArx1sqHGRQBxBBVvysmTdnY6o
In general, it's preference as they're both O sub n operations. This it to say for N number of If's, it'll take on average n/2 times to find the right one, ignoring constants becomes O sub n. Therefore, it doesn't really matter. However, in the OR there is and always will be a return if ONE or MORE is true, however, if the multiple ifs each return a different value, then the OR is inappropriate. If you wish them all to return the same thing, it's easier just to use the OR.
~ Singularity ~
[Image: 9N6Aq1f.gif]
Well, actually what i was thinking is they both are the kind off same family operators and should return the result with the same speed.
(Jan 25, 2013 06:28 PM)jazzycool Wrote:  Well, actually what i was thinking is they both are the kind off same family operators and should return the result with the same speed.

Read my post :P
It confirms your assumption with qualifications as to when and why use which one.
~ Singularity ~
[Image: 9N6Aq1f.gif]
If any condition being true executes the same code then an or statement is both more efficient code-wise and time-wise.

An or statement will stop parsing if it finds a true result. That means if you have a || b || c and a is true, it doesn't check b or c. A series of if statements however will be executed every time no matter what.

If each variable being true needs a separate section of code, then use ifs. If all of them lead to the same code, use or's. If only one needs to be executed if true and the others skipped, then look into else if statements or a switch case.
Is the OR in Actionscript like the case statement in C?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[The Academy] by padrinoJun 30, 2025 04:39 AM 7 258 Jun 30, 2025 04:39 AM
Last Post: Lookdadon
[DBMS] by BosskJan 17, 2018 06:43 AM 4 266 Jan 17, 2018 06:43 AM
Last Post: Bossk
by FailzorroFeb 24, 2017 11:02 AM 0 96 Feb 24, 2017 11:02 AM
Last Post: Failzorro