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) HerongYang.com. All Rights Reserved.
 */
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 with JDK 20. Here is the result:

Initial capacity         128

StringTest                92
StringBufferTest          61
SimpleStringBufferTest    78

I compiled and ran the program with JDK 13. Here is the result:

Initial capacity         128

StringTest               187
StringBufferTest          47
SimpleStringBufferTest    78

Below is the result with JDK 8 on an older computer:

Initial capacity         128

StringTest               132
StringBufferTest          49
SimpleStringBufferTest    47

Below is the result with JDK 1.4 on an older computer:

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:

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

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

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 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

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB