Search This Blog

Loading...

Sunday, May 13, 2012

Java Performance Tips 1 - String Operations


String Operations
There are some performance tips for String manipulation in java, one of them is the concatenation operation.
Concatenation operation can be done by appending the values on the same String object for example:
String str = new String ();
String s = "test";
str+=s;
str+="testString";
The compiler translated this simple line to the following J
str = (new StringBuffer()).append(s).append("testString").toString();
But this method is not preferable (as we will know later), one other method is using StringBuffer .


Using StringBuffer Method
StringBuffer is used to store character strings that will be changed as we know that String class is immutable, so we can concatenate the strings as follows:
StringBuffer sbuffer = new StringBuffer();
sbuffer.append("testString");

StringBuffer vs StringBuilder
Also there is another method to concatenate the String using StringBuilder which is introduced in Java 5, StringBuilder is like the StringBuffer except it is not synchronized, which means that if there are many threads, they can change it in the same time (StringBuilder is not suitable in the multithreading applications).
'Ok, why this stuff for, just for concatenate some strings!' you may ask this question, after running a sample of each and profiling the performance.

Code Example

public class StringOperations {
    public void concatenateUsingString() {
        String str = new String();
        for (int i = 0; i < 10000; i++) {
            str += "testString";
        }
    }
    public void concatenateUsingStringBuffer() {
        StringBuffer sbuffer = new StringBuffer();
        for (int i = 0; i < 10000; i++) {
            sbuffer.append("testString");
        }
    }
    public void concatenateUsingStringBuilder() {
        StringBuilder sbuilder = new StringBuilder();
        for (int i = 0; i < 10000; i++) {
            sbuilder.append("testString");
        }
    }
}

And in the main method, a simple calling to the three methods

public static void main(String[] args) {
        StringOperations soperations = new StringOperations();        
        soperations.concatenateUsingString();
        soperations.concatenateUsingStringBuffer();
        soperations.concatenateUsingStringBuilder();
    }


The result is like the following:
The Results by seconds







The Results by percentage









I have used the Eclipse Test & Performance Tools Platform Project (TPTP) to validate the results, just right click on the project and choose 'Profile As'.

Profile As













Then choose 'ProfileàProfile Configuration '


Profile Configuration


















This result shows the big performance issue of using the String concatenation (Plus operation),
the profiler tells us that the calling of concatenateUsingStringBuffer and concatenateUsingStringBuilder (approximately 0.08%) of time are nothing with respect to concatenateUsingString (99.85% of time).
    http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html
    http://leepoint.net/notes-java/data/strings/23stringbufferetc.html