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: