프로그래밍/Java

Java - String

seungdols 2015. 9. 15. 15:04
public class PerformanceStringCompare {
    private String[] args = {
            "ASD",
            "BCD",
            "WWE",
            "asdfsdfasdfasdfasdfadf",
            "sdfasdfsdfasdfasdf",
            "sdfsdfsdfsdfsfsfd",
            "asdf sdfasdfsd f sdfasdfsdf s sdfs dfsf ",
            "ASD",
            "BCD",
            "WWE",
            "asdfsdfasdfasdfasdfadf",
            "sdfasdfsdfasdfasdf",
            "sdfsdfsdfsdfsfsfd",
            "asdf sdfasdfsd f sdfasdfsdf s sdfs dfsf ",
            "ASD",
            "BCD",
            "WWE",
            "asdfsdfasdfasdfasdfadf",
            "sdfasdfsdfasdfasdf",
            "sdfsdfsdfsdfsfsfd",
            "asdf sdfasdfsd f sdfasdfsdf s sdfs dfsf ",
            "ASD",
            "BCD",
            "WWE",
            "asdfsdfasdfasdfasdfadf",
            "sdfasdfsdfasdfasdf",
            "sdfsdfsdfsdfsfsfd",
            "asdf sdfasdfsd f sdfasdfsdf s sdfs dfsf ",
            "ASD",
            "BCD",
            "WWE",
            "asdfsdfasdfasdfasdfadf",
            "sdfasdfsdfasdfasdf",
            "sdfsdfsdfsdfsfsfd",
            "asdf sdfasdfsd f sdfasdfsdf s sdfs dfsf ",
    };

    public String statement() {
        int length = args.length;
        String result = "";
        for (int i = 0; i < length; i++) {
            result += args[i];
        }
        return result;
    }

    public String statementStringBuilder() {
        int length = args.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(args[i]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        PerformanceStringCompare pc = new PerformanceStringCompare();
        long start = System.currentTimeMillis();
        pc.statementStringBuilder();
        long end = System.currentTimeMillis();
        System.out.println((end - start) + " ms");
    }
}

String의 경우 1ms이지만, StringBuilder의 경우 0ms…
이건 어마어마한 차이다..

반응형