JVM Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
Archived: 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 initial 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 initial 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
Some interesting notes from the results:
Table of Contents
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
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
JRockit JVM 28.2.7 by Oracle Corporation
archived: Download and Install JDK 14 on macOS
Archived: Download and Install Latest JDK 10 on Windows
Archived: Download and Install JDK 1.8.0 on Windows
Archived: Download and Install JDK 1.7.0 on Windows
Archived: Download and Install Java SE 1.6 Update 2
Archived: Installing JRockit JVM 8.0
Archived: Testing with LongWhile.java
Archived: Testing with LongSleep.java
Archived: GCTest.java - Garbage Collection Test Program
Archived: GC Test - Constant Memory Requirement
Archived: GC Test - Periodical Memory Requirement
Archived: GC Test - Releasing Old vs. New Objects
Archived: GC Test - JDK 1.4.0 vs. JDK 1.3.1
Archived: GC Test - Client vs. Server
►Archived: StringBuffer Testing Program
Archived: Installing JRockit JVM 7.0