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 .
//
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;
}
}
use gcc compiler , Generate assembly code ( Without compiler optimization )
.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 $20, %esp
movl $3, -8(%ebp)
cmpl $4, -8(%ebp)
ja .L2
movl -8(%ebp), %eax
sall $2, %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 $0, -12(%ebp)
jmp .L11
.L4:
movl $1, -12(%ebp)
jmp .L11
.L5:
movl $2, -12(%ebp)
jmp .L11
.L6:
movl $3, -12(%ebp)
jmp .L11
.L7:
movl $4, -12(%ebp)
jmp .L11
.L2:
movl $10, -12(%ebp)
.L11:
addl $20, %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
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 .
Technology
Daily Recommendation