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

时间:2013-05-23 20:14:53

标签: string if-statement switch-statement java-6

由于Java 6没有switch-case String,我经常使用枚举将 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会慢一些,因为平均会有更多的比较。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

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