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?
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?
WebSnozz-
Cats have no butt cheeks.
If one farted, then it would make a flute noise.
If there were difference in speed, it would be so small it wouldn't even matter.
*Cela*
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.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.
WebSnozz-
Cats have no butt cheeks.
If one farted, then it would make a flute noise.
Well than what do u suppose it works with? :PI 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. )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
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
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
Have a wonderful day.... and keep smiling... you look terrific that way
signing off...
shiv... as i know him
The switch statement evaluates an expression that yields an integer value and branches to one of a number of labelled sections of code.