Herong's Tutorial Notes on JVM
Dr. Herong Yang, Version 3.00, 2007

Performance of StringBuffer Class

StringBuffer Test Program

In January 2003, Someone posted the following program on Java Developer Connection forum, and reported that the program runs far slower on JDK 1.4.1 compare to JDK 1.3.1:

/**
 * StringBufferTest.java
 * Copied from Java Developer Connection forum, January 2003.
 */
public class StringBufferTest {
   public static void main(String[] args) { 
      // get start time for StringBuffer with no length set
      long t1 = System.currentTimeMillis();
      for (int i = 0; i < 100000; i++) {
         StringBuffer sb1 = new StringBuffer();
         sb1 = updateStringBuffer(sb1);
         String s1 = sb1.toString();
      }
      // Get the finish time for StringBuffer with no length set
      long t2 = System.currentTimeMillis();
      System.out.println("StringBuffer with no inital length took "
         +(t2 -t1) +" milliseconds");
      // get start time for StringBuffer with length set
      long t3 = System.currentTimeMillis();
      for (int i = 0; i < 100000; i++) {
         StringBuffer sb2 = new StringBuffer(237);
         sb2 = updateStringBuffer(sb2);
         String s2 = sb2.toString();
      }
      // Get the finish time for StringBuffer with length set
      long t4 = System.currentTimeMillis();
      System.out.println("StringBuffer with inital length took "
         +(t4 -t3) +" milliseconds");
   }
   public static StringBuffer updateStringBuffer(StringBuffer sb) {
      // Create a string with the string buffer
      // The total length of the string is 237
      sb.append("ACCOUNT_ID");
      sb.append(",");
      sb.append("USER_NAME");
      sb.append(",");
      sb.append("ACCOUNT_TYPE_CD");
      sb.append(",");
      sb.append("ORIGINATION_METHOD_CD");
      sb.append(",");
      sb.append("ORIG_SITE_ID");
      sb.append(",");
      sb.append("AUTH_SITE_ID");
      sb.append(",");
      sb.append("DATA_EXCHANGE_ACCT_FLAG");
      sb.append(",");
      sb.append("FAILED_LOGIN_CT");
      sb.append(",");
      sb.append("CREATE_TS");
      sb.append(",");
      sb.append("LAST_USER_MOD_TS");
      sb.append(",");
      sb.append("LAST_MOD_TS");
      sb.append(",");
      sb.append("LAST_MOD_ACCT_ID");
      sb.append(",");
      sb.append("ACCOUNT_STATUS_CD");
      sb.append(",");
      sb.append("APPROVAL_STATUS_CD");
      sb.append(",");
      sb.append("ADDRESS_ID");
      sb.append(",");
      sb.append("AJB4X_ID");
      return sb;
   }
}

I tried the program on my computer with different initial capacities and got the following result:

JVM                    Capacity   Capacity   Capacity   Capacity
                       Default    237        320        640
HotSpot 1.3.1 Client   1262        781        922       1192
HotSpot 1.4.0 Client   2014       1482       1563       1823
HotSpot 1.4.0 Server   1582        842        981       1202
HotSpot 1.4.1 Client   2043       1533       1582       1873
HotSpot 1.4.1 Server   1703        961        972       1352
JRockit 7.0/1.4.0      1329        704        797       1091

which is very interesting:

  • StringBuffer class indeed performed slower on newer versions of HotSpot.
  • StringBuffer class performed much better on JRockit than HotSpot.
  • StringBuffer class performed slower with initial capacities higher than the length needed.

StringBuffer Testing with JDK 1.6.0

After installing JDK 6u2, I tested the StringBuffer program again on a Windows XP system. Here are the results.

JVM                    Capacity   Capacity   Capacity   Capacity
                       Default    237        320        640
HotSpot 1.6.0 Client   454        313        328        344
HotSpot 1.6.0 Server   407        291        297        312

The results still show that higher initial capacities slow down the performance slightly.

Dr. Herong Yang, updated in 2007
Herong's Tutorial Notes on JVM - Performance of StringBuffer Class