반응형

*StringBuffer이용에 따른 수행 시간

- StringBuffer를 이용한 연산이 일반 연산을 이용할 때보다 결과를 도출해내는데 걸리는 시간이 짧다. (성능 향상에 도움이 된다.)

public class StringBufferPerformanceTest{
    public static void main(String[] args){
        // (1) String의 +연산을 이용해서 10,000개의 *를 이어붙입니다.
        //시작시간을 기록합니다.(millisecond단위)
        long startTime1 = System.currentTimeMillis();
        String str="";
        for(int i=0;i<10000;i++){
            str=str+"*";
        }
        //종료시간을 기록합니다.(millisecond단위)
        long endTime1 = System.currentTimeMillis();
        
        // (2) StringBuffer를 이용해서 10,000개의 *를 이어붙입니다.
        //시작시간을 기록합니다.(millisecond단위)                
        long startTime2 = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<10000;i++){
            sb.append("*");
        }
        //종료시간을 기록합니다.(millisecond단위)
        long endTime2 = System.currentTimeMillis();
        
        // 방법(1)과 방법(2)가 걸린 시간을 비교합니다.
        long duration1 = endTime1-startTime1;
        long duration2 = endTime2-startTime2;
        
        System.out.println("String의 +연산을 이용한 경우 : "+ duration1);
        System.out.println("StringBuffer의 append()을 이용한 경우 : "+ duration2);
    }
}

 

 

반응형

'알고리즘 > 프로그래머스, 백준, 구름' 카테고리의 다른 글

백준 15552 빠른 A+B 자바  (0) 2020.10.21
자바 알람 시계 문제  (0) 2020.10.20
map 자료구조  (0) 2020.06.04
set 자료구조  (0) 2020.06.04
Math 클래스  (0) 2020.06.04

+ Recent posts