The switch with the if.. Else execution efficiency problem

In order to analyze the packet type of the protocol, the interrupt function uses if.. The else statement. At present, there are only two types of packets, which may be increased in the future. Why not use the switch statement? I wonder if it is because of efficiency, after all, we should try to make interrupt handling code more concise and time efficient. So I looked it up and found that the switch statement was actually more efficient than ifelse. The following describes the differences between switch and ifelse in detail. switch… The case with the if… The fundamental difference between else… Case generates a jump table indicating the address of the actual case branch, and the jump table index is equal to the value of the switch variable. Thus, the switch… Case not like if… Else iterate through the conditional branch until it hits the condition, accessing only the entry corresponding to the index number to locate the branch. Specifically, switch… Case will generate a skip table with the size (entries) of the maximum case constant +1. The program first determines whether the switch variable is greater than the maximum case constant. If so, it will jump to the default branch for processing. Otherwise, get the address of the hop table whose index number is the size of the switch variable (that is, the start address of the hop table + the size of the table * the index number), and then jump to this address to complete 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; }

}

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

In this sense, switch is a bit of a space for time trade, and it is. 1. Switch is efficient when there are many branches. Because the switch is random, that is, after the selection value is determined, the specific branch is directly jumped, but if. Else is going to go through so you can get the possible value, until you find the branch that matches the condition. In this case, switch is much more efficient than ifelse. 2. From the above assembly code can know, switch… Case takes up a lot of code space because it generates a skip table, especially if the case constant distribution is large but the actual valid values are small, switch… Case will have very low space utilization. 3.switch… Case can only handle cases where case is constant, and cannot handle very large cases. For example, if (a > 1 && a < 100), it is not possible to use switch… Case. Therefore, switch can only be more efficient than ifelse in constant branch selection, but ifelse can be used in more situations, and ifelse is more flexible.

From this point of view, the previous interrupt handler with switch is more appropriate, that is, to save time, but also very convenient for the extension of the later program. Because the packet type value is basically represented by an integer constant.