Reprint: http://blog.csdn.net/kehui123/article/details/5298337
Switch with If. The efficiency of else's execution
Read a predecessor's program today and find it in the serial interrupt inside in order to analyze the message type of the protocol, use if in the interrupt function. Else statement. Because the type of message appears to be only two now, it is likely to be increased later, uncertain.
I think this is a bit inappropriate, why not switch statements? Speculation is not because of efficiency considerations, after all, we should try to interrupt the processing of the code more concise, more well paid good time efficiency.
So I find the relevant information, the data show that the switch statement is more efficient than the execution of IfElse.
Here's a detailed description of the difference between switch and ifelse.
The fundamental difference between switch...case and If...else is that switch...case generates a jump table to indicate the address of the actual case branch, and the index number of the jump table is equal to the value of the switch variable. Thus, Switch...case does not need to traverse the conditional branch as If...else to hit the condition, but only accesses the table entry of the corresponding index number to reach the location branch.
Specifically, Switch...case generates a table of size (number of table items) as the maximum case constant of +1, and the program first determines whether the switch variable is greater than the maximum case constant, or, if it is greater than, jumps to the default branch processing Otherwise get the address of the tab entry with the size of the switch variable (that is, the start address of the Jump table + table Item Size * index number), the program then jumps to this address to execute, which completes the branch jump.
//
int main ()
{
unsigned int i,j;
i=3;
Switch (i)
{
Case 0:
j=0;
Break
Case 1:
J=1;
Break
Case 2:
j=2;
Break
Case 3:
j=3;
Break
Case 4:
j=4;
Break
Default
j=10;
Break
}
}
Generate assembly code with the GCC compiler (without compiler Optimizations)
. File "Shiyan.c"
. text
. GLOBL Main
. type Main, @function
Main
Leal 4 (%ESP),%ecx
Andl $-16,%esp
Pushl-4 (%ECX)
PUSHL%EBP
MOVL%esp,%EBP
PUSHL%ECX
Subl $,%esp
MOVL, -8 (%EBP)
Cmpl $4, -8 (%EBP)
Ja. L2
Movl-8 (%EBP),%eax
Sall,%eax
Movl. L8 (%eax),%eax
JMP *%eax
. section. Rodata
. Align 4
. Align 4
. L8:
. Long. L3
. Long. L4
. Long. L5
. Long. L6
. Long. L7
. text
. L3:
MOVL, -12 (%EBP)
JMP. L11
. L4:
MOVL $ -12 (%EBP)
JMP. L11
. L5:
MOVL, -12 (%EBP)
JMP. L11
. L6:
MOVL, -12 (%EBP)
JMP. L11
. L7:
Movl $4, -12 (%EBP)
JMP. L11
. L2:
MOVL, -12 (%EBP)
. L11:
Addl $,%esp
POPL%ECX
POPL%EBP
Leal-4 (%ECX),%esp
Ret
. size main,.-main
. Ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
. section. Note. Gnu-stack, "", @progbits
As a result, switch has a bit of space for time, and in fact it does.
1. When there are more branches, the efficiency of using switch is very high. Because switch is randomly accessed, it is determined that the selection value jumps directly to that particular branch, but if. else is the traversal so that it is possible to find the branch that meets the criteria. So, switch is actually much more efficient than ifelse.
2. By the above assembly code can be known, switch...case occupies a lot of code space, because it to generate a skip table, especially when the case constant distribution range is very large but the actual valid value is relatively small, switch...case space utilization will become very low.
3.switch...case can only handle cases where the case is constant, and is powerless in the case of a very limited number of cases. For example, if (a > 1 && a < 100), it is not possible to use switch...case for processing. Therefore, switch can only be used in the constant selection of branches more efficient than ifelse, but IfElse can be applied to more occasions, ifelse more flexible.
Thus, the above predecessors of the interrupt processing program with switch is more appropriate, that is to save time, but also for the future extension of the program is very convenient. Because the value of the message type is basically represented by the shaping constant.
--------------------------------------------------------------------------------------------------------------- ----------------------------
The following comments are also available:
Switch is not always that good, because each calculation will have a two-time table-checking process.
The specific need to see the application scenario, for example: for the network layer of Protocol analysis,%99 may be IP protocol, so basically will be in the first if the hit, only one calculation.
More optimization measures are also likely/unlikely.
In summary: Using switch can improve efficiency for more branching or relatively homogeneous distributions, and using if...if else is better for less branching or uneven distribution.
Switch can only work on those numeric choices of the same type, with a narrow scope; in fact, the IF statement uses more.
The reason is that a lot of continuous judgments of different types, and can not be numerical, and therefore can not be used switch.
For example, write a keyboard button and mouse can move the screen, LZ can write switch judgment?? Of course, you can use switch if it is a keyboard event or a mouse event.
Self-Summary:
If you want to compare the situation in a single case (and match the more dispersed case, excluding the 5 floor) you can use the switch
If you want to compare more cases (such as two compliant statements, or 5 floor), use the IF If...else structure
The efficiency of switch and IfElse