-
"switch" or "if/else" conditional structure??
hello all,
I've been reading a flash games book and the author uses and promotes a lot the "switch/case" conditional system for executing different taskes in games based on the results of a variable, instead of the more traditional if/else/else if conditionals.....
I've used if/else so far excluisively, but I like the form of the code for Switch/case structure and it seems to do the same job in a much more clear way, especially when one has say 10 or 15 tasks to ecxcute based on a variable's value...
what do you think??? is either one faster then the other...is there an adavange to use one over the other???
any help would be appreciated...
thanks,
paul.
-
I see, so you're saying there wouldn't be a difference one the movie is compiled......
and yeah, that's why I was asking...because to me the switch structure looks more organized (especially w/ complex/multiple tasks) then the traditional if/else...
thanks jtnw!
paul.
-
SaphuA
Whuts the switch structure?
-
Senior Member
The switch structure is an efficient way to perform a bunch of 'if' statements when you are testing on the same variable, see the example below. It is indeed faster, as the results below demonstrate.
* * *
I've found the best way to find out which of two methods is faster is to perform an emperical test. Making guesses about how the compiler works, or following other people's guesses will often lead you to erroneous conclusions.
I do these tests all the time.
Here's such a test for your question. Since the single if/switch goes by too fast to time it, we do 10000 of them, inside a loop.
code:
st = getTimer();
for (i = 0; i < 10000; ++i)
{
if (i == 1) a++;
else if (i == 2) b++;
else if (i == 3) c++;
else if (i == 4) d++;
else if (i == 5) e++;
else f++;
}
trace('if version: ' + (getTimer() - st));
st = getTimer();
for (i = 0; i < 10000; ++i)
{
switch (i) {
case 1: a++; break;
case 2: b++; break;
case 3: c++; break;
case 4: d++; break;
case 5: e++; break;
default: f++; break;
}
}
trace('switch version: ' + (getTimer() - st));
And here are the results on my PC:
if version: 146
switch version: 108
So for a 5-element switch statement, the switch statement is clearly faster.
For a 2-element statement, it's also faster, but not by as much.
if version: 96
switch version: 89
The more things you are comparing, the greater the savings. For a simple if/else pair, I wouldn't bother.
Last edited by jbum; 05-09-2004 at 08:54 PM.
-
good stuff, jbum!!!
thanks a lot for testing.....I guess they're not the same after all
paul.
PS: I guess more people are gonna start using switch more often now....
-
Hype over content...
According to ASDG ( Both versions ) switch has no speed gain ( That's why I've never bothered using a switch statement ).
Also in a more real world example, you would split the if checks up if there were a large amount, thereby reducing the total number of checks.
jbum what version of Flash did you publish the tests on mate ? And did you try it the other way round with the switch test first ( Just curious ) ?
[edit] You got in there like a shot jtnw [/edit]
Squize.
-
Senior Member
I tested in MX 2004 on a PC. I tried changing the order of the test and it makes no difference (switch is faster, probably because of the register usage, as jtnw said).
I'd suggest doing the same test on your own machine with code that more closely resembles the way you're using it.
In most situations, the code inside if and switch statements has a much bigger effect on speed than the choice of if or switch.
-
if you have ever done anything BASIC or VBscript.
Case Selects are the most easily executed and the most efficient. Instead of these if/else statements all you need is a Case statement. It's a hell of a lot faster in terms of comparment. However, seeing as how Java uses witch/case and AS and Javascript are almost identical. I would use switch case then if/then staements.
That way in case a code goes wrong you can isolate the problem without worrying about if brackets are set up correctly and stuff.
I was had a lab partner in college who never used case select at all. When he was done putting in if statements he had over 2000 lines of code! I used case and had about 900-1000. So I would teach yourself to use switch/case. It'll save a lot of time.
-
Originally posted by SamusKreations
if you have ever done anything BASIC or VBscript.
The opposite is true if you use C - if/else statements use slightly less code than switch/case selects (depending on how you format your code, natch). For small blocks of code, switch/case will compile in exactly the same way as if/else (though this changes for larger switch/case blocks, and may depend on the behaviour of your chosen compiler).
-
jbum, there is an unfairness in your code. When the if runs, the variables a-f do not exist, so it has to make them, which takes time. the switch/case does not have to make them, so its faster. try to create the varibles before you run it and see which one is faster then.
I find that switch is good for only a few events in the true part, or else it gets confusing. I try to use it sometimes, but i like the if/else if better. That might just be me htough
-
Originally posted by SamusKreations
I was had a lab partner in college who never used case select at all. When he was done putting in if statements he had over 2000 lines of code! I used case and had about 900-1000. So I would teach yourself to use switch/case. It'll save a lot of time.
How could that be? in switch/case you need the break; line pluss the two swithc lines:
code:
switch(i){
case 1:
trace("Its 1");
break;
case 2:
trace("Its 2");
break;
case 3:
trace("Its 3");
break;
case 4:
trace("Its 4");
break;
default:
trace("its "+i);
}
//16 lines
if(i==1){
trace("Its 1");
}else if(i==2){
trace("Its 2");
}else if(i==3){
trace("Its 3");
}else if(i==4){
trace("Its 4");
}else{
trace("Its "+i);
}
//11 lines
-
Originally posted by Ihoss
How could that be? in switch/case you need the break; line pluss the two swithc lines
The code you have quoted is ActionScript, or C. SamusKreations was referring to VB, which is almost entirely different.
-
Flash hates me.
lol! i tried jbums experiment on my laptop and the results were:
if version: 447
switch version: 478
if version was lightly faster on mx
EDIT:
code:
st = getTimer();
for (i = 0; i < 10000; ++i)
{
switch (i) {
case 1: a++; break;
case 2: b++; break;
case 3: c++; break;
case 4: d++; break;
case 5: e++; break;
default: f++; break;
}
}
trace('switch version: ' + (getTimer() - st));
st = getTimer();
for (i = 0; i < 10000; ++i)
{
if (i == 1) a++;
else if (i == 2) b++;
else if (i == 3) c++;
else if (i == 4) d++;
else if (i == 5) e++;
else f++;
}
trace('if version: ' + (getTimer() - st));
this way round:
switch version: 450
if version: 448
then i tried ihoss's idea and defined the variables first, and:
switch version: 452
if version: 447
and:
if version: 452
switch version: 457
Last edited by crashlanding; 05-10-2004 at 12:13 PM.
"wen i found my gerbil dead my other gerbil was eating it i just cried and screamed"
http://www.livescripts.net
--------------------------------------------------------------------------------
Last edited by some moderator : Today at 9:01 PM.
-
So then the if is faster...
-
Hype over content...
This is why I kinda of find these tests pointless. Everyone's machine seems to throw up different results on all of them.
jtnw ( Glad you liked the tut btw, notice it's the last one I've done. It half killed me ), I'm guessing the double not is just compiler junk ( Which the Update feature in FLASM gets rid of anyway ) and I don't know if it does that when using MX 2.4k with the compiler improvements.
Switch and if / else conditionals seem pretty much of muchness, so I think it's a case of personal preference ( Unless it's a lot of conditions to check in one hit, then I'd go for a split if check ).
Squize.
-
Senior Member
I agree with the substance of the previous posts (the results of tests vary on different machines, and it doesn't matter so much whether you use if or switch).
Nonetheless, I am fascinated to try to figure out, thru testing, why crashlanding got such a different ratio in the results. Was it the machine? Is it a PC/Macintosh issue? Or is it a difference between Flash MX 2004 (what I'm using) and Flash MX?
Here is a swf that was output with Flash MX 2004. Would those of you reading this thread tell us what results come up on your machine, when you go to this link? Also tell us what kind of machine you are using, and which web browser.
Switch/If Test (swf)
Also, if one of you with Flash MX (not 2004) would post a SWF with this same code, posted below, I'd be interested to see if I'm getting the same result.
This version preallocates the variables, and does the switch test first.
On my Windows 2000 machine (795 Mhz Pentium III), I get
Switch: 303 If: 423
in Internet Explorer 6
Switch: 258 If: 360
in Mozilla Firefox
Here's the code.
code:
var a,b,c,d,e,f;
_root.createTextField('txt1', 1, 50, 50, 100, 50);
_root.createTextField('txt2', 2, 50, 100, 100, 50);
// SWITCH TEST
st = getTimer();
for (i = 0; i < 10000; ++i)
{
switch (i) {
case 1: a++; break;
case 2: b++; break;
case 3: c++; break;
case 4: d++; break;
case 5: e++; break;
default: f++; break;
}
}
txt1.text = 'switch version: ' + (getTimer() - st);
// IF TEST
st = getTimer();
for (i = 0; i < 10000; ++i)
{
if (i == 1) a++;
else if (i == 2) b++;
else if (i == 3) c++;
else if (i == 4) d++;
else if (i == 5) e++;
else f++;
}
txt2.text = 'if version: ' + (getTimer() - st);
-
Senior Member
-
weird, I got the following w/ jbum's test swf:
switch: 279
if/else: 416
(It keeps changing slightly every time I reload it...but still in that ball park)
using flash mx 2004 player...on a PIII ~800mhz w/ 512mb of ram, Win XP Pro SP2 and IE 6.0 sp 2
however, when I download the file and play it straight off my desktop, I get
swtich: 215
if: 300
why would that be??
paul.
-
how do you mean??
I have player 7 on both the browser and player/ standalone.....is that what you mean?
paul.
-
Senior Member
When you click on the link, you're playing the movie in a web-browser, and flash movies generally don't perform as well in the web browser, because the web-browser is sucking CPU for other stuff.
Both of your ratios, however, are in the same ballpark as I'm seeing on my machine (about 70% give or take a few percent).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|