Java Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 6.00

Performance Comparison of String and StringBuffer

This section provides a tutorial example to do a performance comparison of 3 classes: String, StringBuffer, SimpleStringBuffer.

One main reason of using the StringBuffer class instead of the String class is that StringBuffer performs much better in joining strings together. In order to confirm this, I wrote the following tutorial example to compare performances of 3 classes: String, StringBuffer, and SimpleStringBuffer:

/**
 * StringBufferTest.java
 * Copyright (c) 2003 by Dr. Herong Yang
 */
public class StringBufferTest {
   public static void main(String[] args) {
      int l = 128;
      long t1 = 0;
      long t2 = 0;
      String s = null;
      System.out.println("Initial capacity = "+l);
      for (int i=1; i<=3; i++) {
         System.out.println("Test: "+i);
         t1 = System.currentTimeMillis();
         if (i==1) s = stringTest(l);
         else if (i==2) s = stringBufferTest(l);
         else if (i==3) s = simpleStringBufferTest(l);
         t2 = System.currentTimeMillis();
         System.out.println("   Final string = "+s);
         System.out.println("   Total time = "+(t2-t1));
      }
   }
   public static String stringTest(int l) {
      String s = null;
      for (int i=0; i<100000; i++) {
         String sb = "0";
         sb = sb+"01";
         sb = sb+"012";
         sb = sb+"0123";
         sb = sb+"01234";
         sb = sb+"012345";
         sb = sb+"0123456";
         sb = sb+"01234567";
         sb = sb+"012345678";
         sb = sb+"0123456789";
         s = sb;
      }
      return s;
   }
   public static String stringBufferTest(int l) {
      String s = null;
      for (int i=0; i<100000; i++) {
         StringBuffer sb = new StringBuffer(l);
         sb = sb.append("0");
         sb = sb.append("01");
         sb = sb.append("012");
         sb = sb.append("0123");
         sb = sb.append("01234");
         sb = sb.append("012345");
         sb = sb.append("0123456");
         sb = sb.append("01234567");
         sb = sb.append("012345678");
         sb = sb.append("0123456789");
         s = sb.toString();
      }
      return s;
   }
   public static String simpleStringBufferTest(int l) {
      String s = null;
      for (int i=0; i<100000; i++) {
         SimpleStringBuffer sb = new SimpleStringBuffer(l);
         sb = sb.append("0");
         sb = sb.append("01");
         sb = sb.append("012");
         sb = sb.append("0123");
         sb = sb.append("01234");
         sb = sb.append("012345");
         sb = sb.append("0123456");
         sb = sb.append("01234567");
         sb = sb.append("012345678");
         sb = sb.append("0123456789");
         s = sb.toString();
      }
      return s;
   }
}

I compiled and ran the program on J2SDK 1.4.1 with different initial capacities. Here is the result:

Initial capacity           16     32     64    128

StringTest               1973   1963   1963   1963
StringBufferTest          631    571    500    551
SimpleStringBufferTest    671    611    521    630

The result shows that:

  • The StringBuffer class does perform much better than the String class. The StringBuffer is about 3 times faster!
  • The SimpleStringBuffer class, my implementation of a string buffer class, performs almost the same as the StringBuffer class!

Table of Contents

 About This Book

 Installing JDK 1.4 on Windows 2000

 Installing JDK 1.5 on Windows XP

 Installing JDK 1.6 on Windows XP

 Execution Process, Entry Point, Input and Output

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

StringBuffer - The String Buffer Class

 StringBuffer Class Properties and Methods

 SimpleStringBuffer - My Implementation of String Buffer

Performance Comparison of String and StringBuffer

 System Properties and Runtime Object Methods

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
Performance Comparison of String and StringBuffer