Thursday, October 09, 2008

Java String test

Rumor spreads out that StringBuffer's operations is notably faster than String's.

Let's have some code String vs StringBuffer to see whether the rumor are true or not !

For quick text edit I always use VIM :
$ vim TestStringPerformance.java


Here comes the code:

import java.util.Date;

public class TestStringPerformance {
public static void main(String[] args) {
TestStringPerformance aString = new TestStringPerformance();
aString.testStringAppend(10000);
aString.testStringBufferAppend(10000);
aString.testStringAppend(10000);
aString.testStringBufferAppend(10000);

aString.testStringNew(10000);
aString.testStringBufferNew(10000);
aString.testStringNew(10000);
aString.testStringBufferNew(10000);

aString.testStringReplace5Chars(10000);
aString.testStringBufferReplace5Chars(10000);
aString.testStringReplace5Chars(10000);
aString.testStringBufferReplace5Chars(10000);
}

private void testStringAppend(int loopNum) {
Date startDate = new Date();
String prefixString = "A";
for (int i = 0; i< loopNum; i++) {
prefixString = prefixString + i;
}
Date endDate = new Date();
printTime("StringAppend", startDate, endDate);
}

private void testStringNew(int loopNum) {
Date startDate = new Date();
String newString;
for (int i = 0; i< loopNum; i++) {
newString = "B";
}
Date endDate = new Date();
printTime("StringNew", startDate, endDate);
}

private void testStringBufferAppend(int loopNum) {
Date startDate = new Date();
StringBuffer prefixString = new StringBuffer("A");
for (int i = 0; i< loopNum; i++) {
prefixString.append(i);
prefixString.toString();
}
Date endDate = new Date();
printTime("StringBufferAppend", startDate, endDate);
}

private void testStringBufferNew(int loopNum) {
Date startDate = new Date();
StringBuffer prefixString = new StringBuffer("A");
for (int i = 0; i< loopNum; i++) {
prefixString.replace(0,1,"B");
prefixString.toString();
}
Date endDate = new Date();
printTime("StringBufferNew", startDate, endDate);
}

private void testStringReplace5Chars(int loopNum) {
Date startDate = new Date();
String prefixString = "AAAAABBBBB";
for (int i = 0; i< loopNum; i++) {
prefixString = prefixString.replaceFirst("BBBBB", "BBBBB");
}
Date endDate = new Date();
printTime("StringReplace5Chars", startDate, endDate);
}

private void testStringBufferReplace5Chars(int loopNum) {
Date startDate = new Date();
StringBuffer prefixString = new StringBuffer("AAAAABBBBB");
for (int i = 0; i< loopNum; i++) {
prefixString.replace(0,5,"BBBBB");
prefixString.toString();
}
Date endDate = new Date();
printTime("StringBufferReplace5Chars", startDate, endDate);
}

private void printTime(String methodName, Date startDate, Date endDate) {
long millis = endDate.getTime() - startDate.getTime();
System.out.println(methodName + " took " + millis + " milliseconds ");
}
}


Now let's compile and run to see the result:

$ javac TestStringPerformance.java

$ java TestStringPerformance
StringAppend took 4047 milliseconds
StringBufferAppend took 656 milliseconds
StringAppend took 3235 milliseconds
StringBufferAppend took 656 milliseconds
StringNew took 0 milliseconds
StringBufferNew took 16 milliseconds
StringNew took 0 milliseconds
StringBufferNew took 0 milliseconds
StringReplace5Chars took 203 milliseconds
StringBufferReplace5Chars took 0 milliseconds
StringReplace5Chars took 32 milliseconds
StringBufferReplace5Chars took 0 milliseconds

$ java -version
java version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)


So what ?
The answer is, On my PC and with JRE 1.6 update 6, StringBuffer is much faster than String on "Append" and "Replace" operations, but was slower on first initilization.

I ran the test again:

$ java TestStringPerformance
StringAppend took 3265 milliseconds
StringBufferAppend took 641 milliseconds
StringAppend took 3266 milliseconds
StringBufferAppend took 640 milliseconds
StringNew took 0 milliseconds
StringBufferNew took 16 milliseconds
StringNew took 0 milliseconds
StringBufferNew took 0 milliseconds
StringReplace5Chars took 93 milliseconds
StringBufferReplace5Chars took 0 milliseconds
StringReplace5Chars took 47 milliseconds
StringBufferReplace5Chars took 0 milliseconds

Somehow the time of "Replace" operation is changed: The String ran faster than the first time, whereas the StringBuffer is slower.
However, the final conclusion is the same :D .

What is the result on your PC?

1 comment:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!