StringBuffer와 StringBuilder의 테스트이다.
http://cafe.naver.com/javacircle.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=35391


class A{
 
        private static StringBuffer sbf = new StringBuffer();
        private static StringBuilder sbb = new StringBuilder();
       
        public static synchronized void printValueStringBuffer(int start, int end) {
         for(int i=start; i<end; i++) {
          sbf.append(i);
          sbf.append(",");
         }
         System.out.println(sbf.toString());
        }
        public static synchronized void printValueStringBuilder(int start, int end) {
         for(int i=start; i<end; i++) {
          sbb.append(i);
          sbb.append(",");
         }
         System.out.println(sbb.toString());
        }
  
        public static void main(String args[]) throws Exception{ 
         
//                Thread t1 = new Thread(new CounterStringBuffer(sbf));
//                Thread t2 = new Thread(new CounterStringBuffer2(sbf));
                Thread t1 = new Thread(new CounterStringBuilder(sbb));
                Thread t2 = new Thread(new CounterStringBuilder2(sbb));
               
                t1.start();
                t2.start();
        }
}

/* StringBuffer */
class CounterStringBuffer implements Runnable{
 
 StringBuffer sbf = null;
 
 public CounterStringBuffer(StringBuffer sbf) {
  this.sbf = sbf;
 }
    public void run() {
     A.printValueStringBuffer(50, 100);
    }
}
class CounterStringBuffer2 implements Runnable {
 
    StringBuffer sbf = null;
 
 public CounterStringBuffer2(StringBuffer sbf) {
  this.sbf = sbf;
 }
 
    public void run() {
     A.printValueStringBuffer(1, 50);
    }
}

/* StringBuilder */
class CounterStringBuilder implements Runnable {
 
 StringBuilder sbb = null;
 
 public CounterStringBuilder(StringBuilder sbb) {
  this.sbb = sbb;
 }
 
    public void run() {
     A.printValueStringBuilder(50, 100);
    }
}
class CounterStringBuilder2 implements Runnable {
 
    StringBuilder sbb = null;
 
 public CounterStringBuilder2(StringBuilder sbb) {
  this.sbb = sbb;
 }
 
    public void run() {
     A.printValueStringBuilder(1, 50);
    }
}


====== StringBuffer와 StrinbBuilder의 성능 테스트 ======

public class StringBufferStringBuilderTest {
 
 public static void main(String[] args) {
  StringBuffer stringBuffer = new StringBuffer();
  StringBuilder stringBuilder = new StringBuilder();
  
  String result = null;
  
  long startTime = System.currentTimeMillis();
  for (int i = 0; i < 10000; i++) {
   for (int j = 0; j < 1000; j++) {
    for (char c = 'a'; c < 'z'; c++) {
     stringBuffer.append(c);
    }
   }
   result = stringBuffer.toString();
   stringBuffer = new StringBuffer();
  }
  
  long endTime = System.currentTimeMillis();
  long diff = endTime - startTime;
  System.out.println("diff: " + diff);
  
  //System.out.println("result: " + result);
  
  startTime = System.currentTimeMillis();
  for (int i = 0; i < 10000; i++) {
   for (int j = 0; j < 1000; j++) {
    for (char c = 'a'; c < 'z'; c++) {
     stringBuilder.append(c);
    }
   }
   result = stringBuilder.toString();
   stringBuilder = new StringBuilder();
  }
  
  endTime = System.currentTimeMillis();
  diff = endTime - startTime;
  System.out.println("diff: " + diff);
  
  //System.out.println("result: " + result);
 }
 
}

다음은 3회 실행 결과이다.

diff: 7344
diff: 2766

diff: 6734
diff: 2734

diff: 6734
diff: 2735

StringBuffer는 평균 6937.3 ms의 시간이 소요된데 비해

StringBuilder는 평균 2745 ms의 시간이 소요되었다.

이 경우 StringBuilder가 StringBuffer보다 2.5배 이상 좋은 성능을 보여줌을 확인할 수 있다.

출처 : http://devday.tistory.com/70
 

+ Recent posts