My *nix world

Java string concatenation can be a nightmare

OK, so you are a Java keen programmer and you think that Java rocks. Perhaps you are right. But be aware of Java string concatenation, especially when it's done in a loop where you have hundred thousands or even million of iterations.

Exercise extra caution when choosing a technique for string concatenation in Java written programs. Simply using the "+=" operator to concatenate two strings creates a large number of temporary Java objects, since the Java String object is immutable. This can lead to poor performance (higher CPU utilization) since the garbage collector has additional objects to collect. Use the Java StringBuffer object to concatenate strings because it is more efficient.

The above quote come from an article written in 2001 by some IBM developer.

This is somehow fun, because I've found a code written in 2003 (or alike) by some IBM developers who didn't care to much about the above statement (maybe they were new members of their team :-).

The original code can be found here (as of 2012-09-25):

http://www.jdg2e.com/jdg2e_CD_for_eclipse321/plug-in_development/examples/com.ibm.jdg2e.editor.jfacetext.sql/src-SQLTextEditor/com/ibm/jdg2e/editor/jfacetext/sqleditor/sql/SQLWordStrategy.java

Take a look at the function keyWordsToUpper where they concatenate two strings in a huge loop, that multiplied by the number of tokens that could exists in a SQL file can drive the function to return after....605033 milliseconds (~10 minutes).

I've tested that function against a SQL file that has 20263 lines (~1 MB).

OK, so I've used the IBM developer's advice above and instead of using a plain string concatenation (i.e newContent = newContent + token) I have used the StringBuffer.append(token) method. The result is astonishing: 115 milliseconds. That is more than 5261 times faster than the "same" code, except what I have said already.

Conclusion

Never use string concatenation when you know that the code is targeting a huge loop and/or the concatenated string object can grow uncontrollable (I mean, it does not depend on you, as a programmer but rather by a runtime object, like a file, which can have a variable length).

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

 
The following two tabs change content below.
Java string concatenation can be a nightmare

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.

Leave a Reply

Your email address will not be published. Required fields are marked *