Monday, October 15, 2007

String concatenation with a Java agent

Here are the results from string concatenation in Java:
Standard concatenation (75 000 concatenations): 192.594s, string length, 300 000.

java.lang.StringBuffer-concatenation (75 000 concatenations): 0.062s, string length, 300 000.

RTI-concatenation (75000 concatenations): 1.765s, string length, 300 000.

The best in LS, 75 000 concatenations, 0.25s. The numbers aren't directly comparable, as I'm running on different hardware than Julian.

By "Standard" concatenation, I mean bigString += string. Standard concatenation and RT-concatenation is slower in Java than in LS. Using Java's native StringBuffer class, is a lot faster than any of the competitors in LS (so far).

I'm not surprised that Java's StringBuffer beats Julian's array-based StringBuffer-class in LS, as Sun probably puts a lot more effort into optimization of the language than IBM does with LS.

My test ran on Notes 7.02, which I think runs version 1.4 of the Java runtime. Newer versions of Java may be even faster.

My Java-skills are mediocre at best. Let me know if my test-methodology is wrong in any way.

>> Code for the test

1 comments:

kerr said...

The code may be getting a boost from JVM runtime optimisation since you are appending the same string every time, but the basics are still the best way to do it. I've run similar tests with randomly generated strings that yield similar results.

It should be noted that people can go overboard with using StringBuffer in java, using it everywhere, never using the '+' string operator. This is just a waste of time from the coding and maintenance point of view and doesn't even speed up the code in a lot of cases. Internally java will convert multiple string cats into a StringBuffer anyway, but not in a loop. So the only place you need to use it is a loop.