Comparison of C language switch and if-else Efficiency

Source: Internet
Author: User
Tags switch case

The switch and if-else statements are both conditions in the C language. In terms of syntax, the two functions are the same. If the switch can be completed, the switch can also be completed, however, their application scenarios are slightly different. If is mostly used in a single branch, switch is mainly used in the case of multiple branches.

Let's take a look at the C program and the Assembly Code Compiled with GCC.

/* $ Begin switch-C */
Int switch_eg (int x)
{
Int result = X;

Switch (x ){

Case 100:
Result * = 13;
Break;

Case 102:
Result + = 10;
/* Fall through */

Case 103:
Result + = 11;
Break;

Case 104:
Case 106:
Result * = result;
Break;

Default:
Result = 0;
}

Return result;
}
/* $ End switch-C */

The Code Compiled with GCC is as follows:
. File "switch. c"
. Versions "01.01"
Gcc2_compiled .:
. Text
. Align 4
. Globl switch_eg
. Type switch_eg, @ Function
Switch_eg:
Pushl % EBP
Movl % ESP, % EBP
Movl 8 (% EBP), % edX
Leal-100 (% EDX), % eax
CMPL, % eax
Ja. A9
JMP *. L10 (, % eax, 4)
. P2align 4, 7
. Section. rodata
. Align 4
. Align 4
. L10:
. Long. L4
. Long. Maid
. Long. l5
. Long. L6
. Long. l8
. Long. Maid
. Long. l8
. Text
. P2align 4, 7
. L4:
Leal (% edX, % edX, 2), % eax
Leal (% edX, % eax, 4), % edX
JMP. l3
. P2align 4, 7
. L5:
Addl, % edX
. L6:
Addl, % edX
JMP. l3
. P2align 4, 7
. L8:
Imull % edX, % edX
JMP. l3
. P2align 4, 7
. A9:
Xorl % edX, % edX
. L3:
Movl % edX, % eax
Movl % EBP, % ESP
Popl % EBP
RET
. Lfe1:
. Size switch_eg,. Lfe1-switch_eg
. Ident "GCC: (GNU) 2.95.3 20010315 (release )"
 

In the assembly code above, we can clearly see that the switch part is allocated with a continuous query table, and the discontinuous part of the switch case is also added with corresponding entries, the size of the switch table is not based on the number of case statements, but the gap between the minimum and maximum values of case. When you select a branch, a CMP clause exists first. If the value is greater than the maximum value of the query table, the system jumps to the default clause. The time consumed by all other case statements is O (1 ).

It can be seen that the switch efficiency is generally higher than if-Else
The switch is highly efficient and can be seen from the assembly code.
The switch calculates the value only once and then test, JMP,
If... else is calculated for each condition.
Switch efficiency is independent of the number of branches
When only the branches are small, if efficiency is higher than switch (because switch has a jump table)
If there are many branches, switch is required.
Http://www.ccoder.net/C-yuyantigao/C-yuyan/252.html

Http://www.CCoder.net

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.