Why is the execution efficiency of the switch statement higher than the if-else statement?

When we study flow control statements, it is not difficult to find that in many cases where if-else statements can be used, we can use switch statements instead.

Experienced developers will suggest that we try to use switch statements instead of cumbersome if-else.

The reason for this: the execution efficiency of the switch statement will be higher than that of the if-else statement.

Below we write a simple program to verify it:

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        String aaa = "aaa";
        long t1 = System.nanoTime();
        if("a".equals(aaa)){
    
    
            System.out.println(aaa);
        } else if ("b".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("c".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("d".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("e".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("f".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("g".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("h".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("i".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("j".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("k".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("l".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("m".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else if ("n".equals(aaa)) {
    
    
            System.out.println(aaa);
        } else {
    
    
            System.out.println(aaa);
        }
        long t2 = System.nanoTime();
        System.out.println("if 语句执行时间: " + (t2 - t1));

        //switch语句测试代码:
        long tt1 = System.nanoTime();
        switch (aaa) {
    
    
            case "a":
                System.out.println(aaa);
                break;
            case "b":
                System.out.println(aaa);
                break;
            case "c":
                System.out.println(aaa);
                break;
            case "d":
                System.out.println(aaa);
                break;
            case "e":
                System.out.println(aaa);
                break;
            case "f":
                System.out.println(aaa);
                break;
            case "g":
                System.out.println(aaa);
                break;
            case "h":
                System.out.println(aaa);
                break;
            case "i":
                System.out.println(aaa);
                break;
            case "j":
                System.out.println(aaa);
                break;
            case "k":
                System.out.println(aaa);
                break;
            case "l":
                System.out.println(aaa);
                break;
            case "m":
                System.out.println(aaa);
                break;
            case "n":
                System.out.println(aaa);
                break;
            default:
                System.out.println(aaa);
                break;
        }
        long tt2 = System.nanoTime();
        System.out.println("switch 语句执行时间: " + (tt2 - tt1));
    }
}

Output:

aaa
if 语句执行时间: 201300
aaa
switch 语句执行时间: 18200

We can find that in this program, the execution time of the swicth statement is only one twentieth of the if-else statement.

From this program is enough to reflect the efficiency of the switch statement.

So why is the switch statement so much more efficient than the if-else statement?

This is because the compiler generates a jump table when processing the switch statement, and then jumps between values ​​according to the value. However, for if-else statements, the compiler needs to compare one by one until it finds the result.

From the perspective of data structure and algorithm, the switch statement is equivalent to an array, and its query time complexity is O(1); while the if-lese statement is equivalent to a linked list, and its time complexity is O(n) .

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/111501222