The efficiency of switch and ifelse

switch... Case and if The fundamental difference of else is that switch Case will generate a jump table to indicate the address of the actual case branch, and the index number of this jump table is equal to the value of the switch variable. Thus, switch Case does not need to be like if Else traverses the conditional branch until it hits the condition, and only needs to access the table item corresponding to the index number to locate the branch.

Specifically, switch... Case will generate a skip table whose size (number of items) is the maximum case constant+1. The program first judges whether the switch variable is greater than the maximum case constant. If it is greater than the maximum case constant, it will skip to the default branch for processing; Otherwise, obtain the address of the jump table item whose index number is the size of the switch variable (that is, the start address of the jump table+the size of the table item * the index number), and then the program jumps to this address to execute, thus completing the branch jump.


From this point of view , switch is a bit of space for time In fact, it is true.


1. When there are many branches, the efficiency of using switch is very high. Because the switch is accessed randomly, that is, after the selection value is determined, it will jump to that specific branch directly, but if.. Else is the possible value of traversal, knowing that the branch matching the condition is found. In this way, the efficiency of switch is much higher than that of ifelse.
2. As can be seen from the assembly code above, switch Case takes up more code space, Because it needs to generate jump tables , especially when case constants have a large distribution range but few actual effective values, switch The space utilization rate of case will become very low.
3.switch... Case can only handle cases where case is a constant, and can do nothing for non constant cases. For example, if (a>1&&a<100), the switch cannot be used Case. Therefore, the switch can only be more efficient than ifelse when the constant selects branches, but ifelse can be applied to more occasions, and ifelse is more flexible.

 

1. Switch is used to make multiple branches according to an integer value, and the compiler can optimize multiple branches
two Switch case calculates the expression only once, then compares the value of the expression with the value of each case, and then selects
Select the statement block of which case to execute
three If.. Else has a wide range of judgment conditions, each statement is basically independent, and each judgment requires conditional loading
Once.
Therefore, switch is used to compare if else if .. The else structure should be efficient.


First, let's look at a problem. If statements can be used in a wide range of applications. All boolean expressions can be judged by if; Switch can only perform numerical comparison on basic types. The comparability of the two is limited to the comparison of the two basic types.
When it comes to numerical comparison of basic types, there must be two numbers. Then the point came——
Each if statement is independent. See the following statement:
if (a == 1) ...
else if (a == 2) ...
In this way, a will be read into the register twice, and 1 and 2 will be read into the register once respectively. So do you find that it is a bit redundant to read a twice. You only need to read in the register once before you have finished all the comparisons. The rest is extra overhead. But the if statement must take the two numbers out of memory and read them to the register every time. It doesn't know that you are comparing the same a.
Then the switch case came out and changed the above version to the switch case version:
switch (a) {
        case 0:
                break;
        case 1:
}

three
Because when the virtual machine reads the switch, it loads all the judgment data into memory, while if loads while judging, it is slower,
Generally, if there is not much data and it is byte, short, int or char type, switch is usually used, which is more efficient

In Java (C doesn't know): If the value in the case is sparse, use lookupswitch:

You can see the
 3: lookupswitch{ //4
3: 44;
20: 55;
50: 66;
100: 77;
default: 85 }
This is about to look up the table next to determine the jump position.

posted @ 2016-08-09 08:43   balingybj   Reading( nineteen thousand four hundred and sixty Comments( two edit   Collection   report