switch And if..else The efficiency of the implementation of
Read the program of the first generation today , It is found in the serial port interrupt to analyze the message type of the protocol , Use in interrupt function if..else sentence . Because there are only two message types , It may increase in the future , uncertain .
I don't think it's proper , Why not switch What about the statement ? Guess if it's because of efficiency , After all, we should try our best to make the interrupt handling code more concise , Better time and efficiency .
So I look up the relevant information , Data display switch Statement is more than ifelse High efficiency of implementation .
Here is a detailed description switch And ifelse The difference between .
switch...case And if...else The fundamental difference is ,switch...case A jump table is generated to indicate the actual case Address of branch , And the index number of this jump table is switch The values of variables are equal . thus ,switch...case Not like if...else So we're going through the conditional branches until we hit the condition , It only needs to access the table entry corresponding to the index number to locate the branch .
speak specifically ,switch...case A size will be generated （ Number of table items ） Is the largest case constant ＋1 Jump table of , Procedure first switch Is the variable greater than the maximum case
constant , If greater than , Then jump to default Branch processing ; Otherwise, get the index number as switch Address of the jump table item of variable size （ The starting address of the hop table ＋ Table item size ＊ Index number ）, The program then jumps to this address to execute , This completes the branch jump .
unsigned int i,j;
use gcc compiler , Generate assembly code （ Without compiler optimization ）
.type main, @function
leal 4(%esp), %ecx
andl $-16, %esp
movl %esp, %ebp
subl $20, %esp
movl $3, -8(%ebp)
cmpl $4, -8(%ebp)
movl -8(%ebp), %eax
sall $2, %eax
movl .L8(%eax), %eax
movl $0, -12(%ebp)
movl $1, -12(%ebp)
movl $2, -12(%ebp)
movl $3, -12(%ebp)
movl $4, -12(%ebp)
movl $10, -12(%ebp)
addl $20, %esp
leal -4(%ecx), %esp
.size main, .-main
.ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
From this point of view ,switch It's a little space for time , And it's true .
1. When there are many branches , At that time switch It's very efficient . because switch It's random , It is to jump to the specific branch after determining the selection value , however if..else It's ergodic, so it's possible , Know to find eligible branches . So it seems ,switch The efficiency ratio of ifelse Much higher .
2. You can see from the assembly code above ,switch...case Occupy more code space , Because it's going to generate a jump table , Especially when case The case that the constant distribution range is large but the actual effective value is relatively small ,switch...case Space utilization will become very low .
3.switch...case Can only be processed case Constant case , There's nothing we can do about extraordinary situations . for example if (a > 1 && a <
100), Is not available switch...case To deal with . therefore ,switch Can only be compared when a constant selects a branch ifelse efficient , however ifelse Can be applied to more occasions ,ifelse More flexible .
From this point of view , Used in the interrupt handler of the previous generation switch It's more appropriate , Save time , It is also very convenient for the extension of later programs . Because the value of message type is basically represented by an integer constant .