Pages

2014년 12월 11일 목요일

[JAVA] StringBuffer와 StringBuilder 성능 비교

StringBuilder와 StringBuffer를 비교하는 프로그램을 만들 수 있습니다. 

package com.littletutorials.tips;
public class ConcatPerf {
    private static final int ITERATIONS = 100000;
    private static final int BUFFSIZE = 16;

    private void concatStrAdd() {
        System.out.print("concatStrAdd   -> ");
        long startTime = System.currentTimeMillis();
        String concat = "";
        for (int i = 0; i < ITERATIONS; i++) {
            concat += i % 10;
        }
        long endTime = System.currentTimeMillis();
        System.out.print("length: " + concat.length());
        System.out.println(" time: " + (endTime - startTime));
    }

    private void concatStrBuff() {
        System.out.print("concatStrBuff  -> ");
        long startTime = System.currentTimeMillis();
        StringBuffer concat = new StringBuffer(BUFFSIZE);
        for (int i = 0; i < ITERATIONS; i++) {
            concat.append(i % 10);
        }
        long endTime = System.currentTimeMillis();
        System.out.print("length: " + concat.length());
        System.out.println(" time: " + (endTime - startTime));
    }

    private void concatStrBuild() {
        System.out.print("concatStrBuild -> ");
        long startTime = System.currentTimeMillis();
        StringBuilder concat = new StringBuilder(BUFFSIZE);
        for (int i = 0; i < ITERATIONS; i++) {
            concat.append(i % 10);
        }
        long endTime = System.currentTimeMillis();
        System.out.print("length: " + concat.length());
        System.out.println(" time: " + (endTime - startTime));
    }

    public static void main(String[] args) {
        ConcatPerf st = new ConcatPerf();
        System.out.println("Iterations: " + ITERATIONS);
        System.out.println("Buffer    : " + BUFFSIZE);

        st.concatStrBuff();
        st.concatStrBuild();
        st.concatStrAdd();
    }
}

결과 
Iterations: 100000
Capacity : 16
concatStrBuff -> length: 100000 time: 16
concatStrBuild -> length: 100000 time: 15
concatStrAdd -> length: 100000 time: 10437
위 결과를 보듯이 일반 문자열 연결은 상당한 시간이 소요된다는 것을 알 수 
있습니다. 컴파일러는 concatStrAdd() 메소드를 를 수행할때 StringBuilder를 
사용하고 있습니다. 
하지만 이는 For문이 돌때 마다 Instance를 하나씩 만듭니다. StringBuffer와 
StringBuilder를 위해 초기 값을 크게하면 큰 차이가 없습니다. 분명히, 10만번의 
반복에서 StringBuffer와 StringBuilder의 수행결과는 별 차이가 없습니다.
그렇다면 Iterations 값을 100000000 (1억) 으로 하고 수행을 해보면 결과가 아래와 
같이 나옵니다. 물론 concatStrAdd()는 수행에서 제외 합니다. 끝나는 시간이 상당히 
오래 걸릴 것이기 때문입니다. 
결과
Iterations: 100000000
Capacity : 16
concatStrBuff -> length: 100000000 time: 15142
concatStrBuild -> length: 100000000 time: 10891
StringBuilder가 동기화를 피하기 때문에 확실히 더 빠르다는 것을 알 수 있습니다.
그럼 BufferSize를 100000000 으로 하고 다시 테스트를 하면 아래와 같은 결과를 
얻을 수 있습니다.
결과
Iterations: 100000000
Capacity : 100000000
concatStrBuff -> length: 100000000 time: 14220
concatStrBuild -> length: 100000000 time: 10611
StringBuilder가 평균적으로 보면 30% 정도의 속도가 빠르다는 것을 알 수 있습니다.
하지만 기억해야 할 것은 Thread safe 하지 않다는 것입니다.
Thread safe 관련 => Thread safe와 none Thread safe의 비교

댓글 없음:

댓글 쓰기