Does a switch case any sort of optimization(in VC++ 6) to make it faster than if else, or is it just easier to code?
Printable View
Does a switch case any sort of optimization(in VC++ 6) to make it faster than if else, or is it just easier to code?
If there were difference in speed, it would be so small it wouldn't even matter.
Case had a disatvantage over if but i forget it at the moment...
IF can be used with variable types that switch cannot. As far as speed difference, as said before it'd be so small you'd never notice it. Sometimes a program needs a switch, sometimes it needs an if, sometimes it needs both. Its all about implementation.Quote:
Originally posted by WebSnozz
Does a switch case any sort of optimization(in VC++ 6) to make it faster than if else, or is it just easier to code?
I can't imagine a program NEEDING case or if?
Can you give an example?
well you can always use macros, i'm talking like obvious things such as:
if (variable == something)
{
...
}
I don't like case anyway because it looks like basic
I perfer a switch in cases such as character input.
We had this debate in class today, ironically. Some kid said, stupidly, "Well why can't i just write my program with a bunch of if statements?" The assignment consisted of about 25 checks on user input, 50% of which were character, and imo to use all ifs or a mojority of ifs wouldn't be efficent.
I thought that since the switch case doesn't work with variables, then it's not checking each and every variable to see does (switchVariable == case), but instead is using maybe some sort of boolean search, where all the case values are organized into a red black tree at compile time. If this, or something similar, were the case, then performance would be greatly improved when you have a large amount of case statements versus the same number of if else statements. If it does use a red black tree search, case would be O(log2 n) versus O(n) for if else. Which will go from a small difference to a huge difference in speed if you get more and more values you need to check.
Also any small amount of improved performance is multiplied many times whenever it is in a frequently executed part of the code.
It's also possible that if else statements and switch case compile into very similar machine code. But it would seem if that were true, then variables could be used as the key value in the case statement.
Well than what do u suppose it works with? :PQuote:
I thought that since the switch case doesn't work with variables
Code:char im_a_variable;
some cout for a letter
switch(im_a_variable)
{
...
}
Agreed. (However, not with the spelling. :) )Quote:
Well than what do u suppose it works with?
Switch statements work with integral values only. Doesn't mean they're not "variables".
Let's be careful of the 'basics'.
It's incumbent upon the programmer to know when a series of IF statements are preferred to a SWITCH statement.
Execution speed, in this particular instance, should play a secondary role to "readability". Frequently repeated code should be functionalized. Ergo, we're back to "readability".
My opinion...
-Skipper
Glad you asked! I was once curious about this too, and I made my own speed test:
Testing...
If: 4806
Switch: 2464
Op. If: 761
<cut off from other tests>
It really depends on the way you program that block of code. Each test is done 50000000 times on a P2 366Mhz laptop with music running, lol. The times are in milliseconds.
This is the If:
if (n==0)
n++;
else if (n==1)
n++;
else if (n==2)
n++;
...
else if (n==18)
n++;
else
n=0;
This is the switch:
switch (n)
{
case 0:
n++;
break;
case 1:
n++;
break;
...
case 18:
n++;
break;
default:
n=0;
}
This is the optimized if:
if ((n++)==19)
n=0;
BTW, I ran the test various times and the same results appeared. This was done on MS C++.NET (unmanaged code (normal, native)).
1 S) Switch case statements work only with integral types
1 F) If statements work with all data types
2 S) Every case statement must be followed by a constant
2 F) If statements can work with constants / variables / expressions
3 S)Switch case allows for equality check only (==) w.r.t switch expression and the case constant
3 F) If statements support all relational / logical operators
The switch statement evaluates an expression that yields an integer value and branches to one of a number of labelled sections of code.
Thanks Speedy5.
What I meant when I said it doesn't work with variablesis that the case statements can't contain variables, they have to be constants. The only reason I see that it must be a constant would be that it is storing the values in some sort of organized format that allows the case values to be searched, instead of checking each and every value for equality. I don't think it's actually going from case to case and saying is the Switch variable == to the case value, but instead is just doing some sort of tree search, which would be much faster, especially when you have a whole lot of values.
It is obvious there is a huge speed difference from Speedy5's test. And if you increase the number of values then an if else would slow down linearly because of it's O(n), but the case would probably not slow down nearly as much because of it's O(log2 n) running time.