-
"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 belive they're both compiled into the same bytecode, switch case just looks neater in AS.
jtnw
-
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?
-
code: switch (number) {
case 1:
trace ("number==1");
break;
case 2:
trace ("number==2");
break;
case 3:
trace ("number==3");
break;
default:
trace ("number!=1,2, or 3")
}
jtnw
-
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....
-
I ran it through Flasm, and switch is actually faster since Flash stores the variable in a register.
I wasn't making a guess on how the compiler works, I'm pretty sure someone posted that they were the same in the optimization thread. Are you testing under MX or MX2k4?
jtnw
-
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.
-
I just looked closer at the p-codes, and if you were to store 'i' as a register, the only speed difference would be that if/else uses an extra 'not'. The advantage to using if/else would be as squize said, you can group them together making it faster than a switch.
You got in there like a shot jtnw
ehheh, I owe it to you anyway since you got me interested in flasm with your tut.
jtnw
Last edited by DELETED_jtnw; 05-09-2004 at 08:24 PM.
-
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.
-
Squize, it was actually a single not. The way the two differ is that switch checks if the number is equal to the condition, and if it's true, it branches to the corresponding part of the code. If/else checks to see if the number isn't equal to the condition, if it isn't then it branches to the next check. So that's where the extra not comes from.
I agree with Squize, either will work fine.
jtnw
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
|