JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

StringBuffer Testing Program

This section describes a StringBuffer testing programs and test results showing that JDK 1.4.1 is slower than JDK 1.3.1.

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.

Last update: 2003.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

StringBuffer Testing Program and Result

StringBuffer Testing Program

 StringBuffer Testing Result with JDK 1.6.0

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
StringBuffer Testing Program