繁体   English   中英

为什么if-else for String比switch-case for enum更快?

[英]Why is if-else for String faster than switch-case for enum?

由于Java 6没有String switch-case ,我经常使用enum将if-else块更改为switch-case,如下面的代码所示。 然而,当我试图检查两种替代方案的性能时,我发现switch-case比if-else替代方案慢,这与我的预期相反。 以下是我在下面的代码中获得的一些结果

Iterations  If-Else   Switch-Case
1           11810      1609181
10           8214      1059115
100         24141      1152494
1000           183975      1580605
10000         4452698      8710648
100000        7069243     19457585

package conditionals;

import java.util.Random;


public class StringConditionalCheck {

    private static int ifElseCounter = 0;
    private static int switchCaseCounter = 0;

    private static final String first = "First";
    private static final String second = "Second";
    private static final String third = "Third";
    private static final String fourth = "Fourth";

    enum StringOptions {
        First, Second, Third, Fourth
    }

    public static void main(String[] args) {
        final int iterations = Integer.parseInt(args[0]);
        String[] userInputs = generateUserInputs(iterations);

        // Using if-else
        long ifelseStartTime = System.nanoTime();       
        for(int i=0; i<iterations; i++){
            useIfElse(userInputs[i]);
        }       
        long ifelseEndTime = System.nanoTime();
        long ifElseDuration = ifelseEndTime - ifelseStartTime;


        long switchcaseStartTime = System.nanoTime();
        for(int i=0; i<iterations; i++){
            useSwitchCase(userInputs[i]);
        }
        long switchcaseEndTime = System.nanoTime();     
        //just to verify that both options had the same result.
        long switchcaseDuration = switchcaseEndTime - switchcaseStartTime;
        System.out.println(iterations + " " + ifElseDuration + " " + switchcaseDuration + " " + ifElseCounter + " " + switchCaseCounter);
    }

    private static String[] generateUserInputs(int numberOfInputs) {
        String[] generatedInputs = new String[numberOfInputs];
        String[] inputsToChooseFrom = new String[]{first, second, third, fourth};
        Random r = new Random();
        for (int i = 0; i < numberOfInputs; i++) {
            int choice = r.nextInt(4);
            generatedInputs[i] = inputsToChooseFrom[choice];
        }
        return generatedInputs;
    }

    public static void useSwitchCase(String input) {
        StringOptions option = StringOptions.valueOf(input);
        switch(option){

        case First:
            switchCaseCounter += 1;
            break;

        case Second:
            switchCaseCounter += 2;
            break;

        case Third:
            switchCaseCounter += 3;
            break;

        case Fourth:
            switchCaseCounter += 4;
            break;          
        }
    }

    public static void useIfElse(String input) {
        if(input.equals("First")){
            ifElseCounter += 1; 
        }else if(input.equals("Second")){
            ifElseCounter += 2;
        }else if(input.equals("Third")){
            ifElseCounter += 3;
        }else if(input.equals("Fourth")){
            ifElseCounter += 4;
        }
    }
}

造成这种差异的原因是什么? 我期待if-else会变慢,因为平均会有更多的比较。

因为99%的时间都花在StringOptions option = StringOptions.valueOf(input); ,而不是在switch

基于源代码,StringOptions.valueOf调用执行几个复杂的操作,包括每次调用构建HashMap,而String.equals只是循环遍历字符串一次。

Enum.valueOf (调用Enum.getConstantDirectory)的源代码与String.equals进行比较

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM