The efficiency of switch and ifelse

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 this jump table is equal to the value of the switch variable. Thus, the switch... case does not need to traverse the conditional branch until it hits the condition as if... else does, but only needs to access the table item corresponding to the index number to locate the branch.

Specifically, Switch... case will generate a skip table with the size (number of items) of 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 means a little 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. From the assembly code above, switch... case takes up more code space, Because it needs to generate jump tables , especially when the distribution range of case constants is large but the actual effective values are relatively small, the space utilization rate of switch... case will become very low.
3. Switch... case can only deal with cases that are constant, but can't do anything about non constant cases. For example, if (a>1&&a<100) cannot be processed with switch... 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, switching is more efficient than if.. Else if.. Else structure when multi branching.


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 finish 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 six hundred and twenty-two Comments( two   Collection   report