![]() |
|
[Help] [ActionScript] What is faster? Multiple IF statements or OR? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5) +---- Thread: [Help] [ActionScript] What is faster? Multiple IF statements or OR? (/showthread.php?tid=3206527) |
[ActionScript] What is faster? Multiple IF statements or OR? - Phyar - Jan 25, 2013 Which is faster? Code if (something1 == true) {OR Code if ((something1 == true) || (something2 == true) || (something3 == true)) {* - vitafortis - Jan 25, 2013 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. * - jazzycool - Jan 25, 2013 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. * - vitafortis - Jan 25, 2013 (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. * - Super Effective - Jan 25, 2013 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. * - Gpure - Jan 25, 2013 Is the OR in Actionscript like the case statement in C? |