The efficiency of switch and IfElse

Source: Internet
Author: User
Tags switch case

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.


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.

The 1.switch is used to route multiple branches based on an integer value, and the compiler can optimize the Multipath branch
2.Switch-case evaluates the expression only once, and then compares the value of the expression to the value of each case, and then
the statement block that selects which case to execute
3.if: Else a wide range of judging conditions, each statement is basically independent, every time you judge the condition to load
one time.
so use the switch ratio if in the multi-channel branch. else if: Else structure is highly efficient.


the first thing to look at is that if the application scope is wide, as long as the Boolean expression can be judged by the IF, and switch can only make a numeric comparison of the base type. The comparability of the two is limited to the comparison of two basic types.
when it comes to numerical comparisons of basic types, of course there are two numbers. And then the point is--
each sentence of the IF statement is independent, see the following statement:
if (a = = 1) ...
else if (a = = 2) ...
so that a is read into the register two times, 1 and 2 are read into the register one at a time. so did you find that actually a read two times is a bit redundant, you only need to read the register one time before all the comparison is complete, the rest is the extra cost. But the IF statement has to take the two numbers out of memory each time to read the registers, it does not know that you are actually comparing the same a.
So switch case comes out and changes the above into a switch case version:
switch (a) {
Case 0:
Break ;
Case 1:
}

3.
because when the virtual machine reads the switch, all the judgment data is loaded in memory, and if is the edge to determine the load, so slow,
generally, if the data is not much, and is byte,short,int or char type when the general use of switch, that efficiency is higher.

In Java (C does not know): if the value in case is sparse, use Lookupswitch:

Can see one of the
3:lookupswitch{//4
3:44;
20:55;
50:66;
100:77;
DEFAULT:85}
This will be next to the table to determine the jump position.

The efficiency of switch and IfElse

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.