[cp-patches] FYI: Permission minor fix

Tom Tromey tromey at redhat.com
Thu Jan 11 15:08:14 UTC 2007

>>>>> "Tania" == Tania Bento <tbento at redhat.com> writes:

Tania> I've written a quick benchmark that shows the difference
Tania> between using string concatenation, StringBuffer and
Tania> StringBuilder.

Note that "String +" is implemented by Java compilers by translating to
StringBuilder (1.5 compilers) or StringBuffer (all earlier compilers).

However, most compilers only do this within a single expression.  So...

Tania>    String actions = "";
Tania>    if (!getActions().equals(""))
Tania>       actions = ' ' + getActions() + ' ';
Tania>    String string = '(' + getClass().getName() + ' ' 
Tania>                    + actions + getName() + ')';
Tania>    return string;

... this code will most likely be turned into multiple 'new StringBuilder's,
and thus be slower than writing it out by hand.

If you rewrite it as a single expression you can look at the resulting
code and see that it is equivalent.

As a general rule I would say that code should be written for clarity
and not for micro-optimization.  There are exceptions, of course,
e.g. cases where we know that a particular bit of code is
performance-critical.  In these cases I would expect documentation of
the weirdness.


More information about the Classpath-patches mailing list