robert burrell donkin wrote:
On 11/14/06, Stefano Bagnara <[EMAIL PROTECTED]> wrote:
Maybe this helps on this topic:
http://wiki.java.net/bin/view/Javapedia/AlwaysUseStringBufferMisconception#Concatenating_Literal_Strings

note that the misconception the link talks about is concatinating
string literals, not variables

use of string buffers will be quicker and use less memory than
explicit string assignments when doing variable composition (as is
typically the case when logging)

- robert

I took 5 minutes to do a synthetic test about this issue: I'm aware that synthetic tests have to analyzed carefully, but this one seems to be really simple. bar and foo are 2 String variable defined in the code just before. They are *not* final.

Input code 1:
> StringBuffer resb = new StringBuffer();
> resb.append(foo).append("test").append(bar).append("test").append(i);
> String res = resb.toString();
----
Input code 2:
> String res = foo + "test" + bar + "test" + i;
----

Output code 1 from eclipse 3.2.1
> StringBuffer resb = new StringBuffer();
> resb.append(foo).append("test").append(bar).append("test").append(i);
> String s = resb.toString();
----
Output code 1 from sun jdk 1.4
> StringBuffer stringbuffer = new StringBuffer();
> stringbuffer.append(s).append("test").append(s1)
>     .append("test").append(i);
> String s2 = stringbuffer.toString();
----
Output code 1 from sun jdk 5
> StringBuffer stringbuffer = new StringBuffer();
> stringbuffer.append(s).append("test").append(s1)
>     .append("test").append(i);
> String s2 = stringbuffer.toString();
----

Output code 2 from eclipse 3.2.1
> String s = foo + "test" + bar + "test" + i;
----
Output code 2 from jdk 1.4
> String s2 = s + "test" + s1 + "test" + i;
----
Output code 2 from jdk 1.5
> String s2 = (new StringBuilder()).append(s).append("test")
>     .append(s1).append("test").append(i).toString();

I also ran with jdk5 that code in a loop of 1 million iterations using foo = "foo" and bar = "bar" (this is FAR from any realistic scenario).

JRE 1.5.0 Windows
Test1_*.class          => 850 - 1020 ms
Test2_eclipse321.class => 880 - 1010 ms
Test2_sunjavac14.class => 830 - 1010 ms
Test2_sunjavac15.class => 780 - 890 ms

JRE 1.4.2 Windows
Test1_sunjavac14.class => 1080 - 1210 ms
Test2_sunjavac14.class => 1050 - 1250 ms

I also tried using jdk1.5 using target 1.4 and the resulting class equal sto the jdk1.4 generated class.

This is really a simple test, but my conclusion is that if we compile with jdk 1.5 it is better to use real string concatenation even when we have variables and not only literals. It is interesting that using jdk1.5 to compile it will automatically use a StringBuilder instead of the StringBuffer.

Either way it seems that given the timings at least in a synthetic test the jvm will optimize the loop even when the code has been compiled as a simple string concatenation.

After all I think we should keep using the getLogger().isXXXXEnabled() but we could start using direct string concatenation when we can do this in a single line. It seems that using jdk1.4 results are comparable while using jdk1.5 it is even better to use the direct string concatenation. If we add that direct string concatenation is so much better to read....

Stefano

PS: it took much more time to write this mail than to write the tests ;-)


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to