Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7818

Re: StringBuilder

Date 2011-09-11 16:48 -0400
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: StringBuilder
References <96f358c8-a024-40db-b60b-300186c2f813@o10g2000vby.googlegroups.com> <j41fik$3qb$1@news.albasani.net> <4e655caa$0$308$14726298@news.sunsite.dk> <MPG.28d6e5a3ee3006899896b2@202.177.16.121>
Message-ID <4e6d1eb6$0$306$14726298@news.sunsite.dk> (permalink)
Organization SunSITE.dk - Supporting Open source

Show all headers | View raw


On 9/11/2011 10:35 AM, Wanja Gayk wrote:
> In article<4e655caa$0$308$14726298@news.sunsite.dk>, arne@vajhoej.dk
> says...
>>> Whereby this code is slower:
>>>
>>> String res="";
>>> for (int i=0; i<100; i++) {
>>> res+=i+"*"+i+"="+(i*i)+"\n";
>>> }
>>> System.out.println(res);
>>>
>>> It is translated to the following code by the compiler, and
>>> thus uses 100 new and 100 toString():
>>>
>>> String res="";
>>> for (int i=0; i<100; i++) {
>>> StringBuilder _buf=new StringBuilder(res);
>>> _buf.append(i);
>>> _buf("*");
>>> _buf.append(i);
>>> _buf.append("=");
>>> _buf.append((i*i));
>>> _buf.append("\n");
>>> res=_buf.toString();
>>> }
>>> System.out.println(res);
>>>
>>> For more information see for example here:
>>> http://caprazzi.net/posts/java-bytecode-string-concatenation-and-stringbuilder/
>>
>> That has been known for 10-15 years.
>>
>> It should be in any Java book above beginners level.
>
> Like other ancient performance-practices that have been obsoleted by
> today's compilers?

No.

What we are talking about is the "+=" part and that part is
still relevant for todays compilers.

Actually JB's example use = but to be equivalent to the first code
the it need to be +=.

And besides what he mention as reasons there are also
the 100 new strings.

> We have been told that using StringBuffer was the better alternative.
> Now compilers have switched from using StringBuffer for the above
> example to the unsynchronized StringBuilder. Those who have manually
> used the StringBuffer have stopped the compiler for doing that for them
> and must rely on the JITs lock elision algorithm.

That is a little interesting quirk.

The outside loop string += part is more efficient with StringB*. And
I am pretty sure that the StringBuffer/StringBuilder difference
is negligible.

But the inside loop string + is probably a little bit faster
with StringBuilder than StringBuffer.

You can consider that insignificant compared to the first.

Or you could argue for using StringB* and append of string +.

> So as long as this part of the code does not represent a critical
> performance-bottleneck, I would recommend to use the simple, stupid
> "slow" variant and hope for future compilers to detect and optimize that
> pattern.

As long as it is not critical then readability should be the deciding
factor.

Arne

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

StringBuilder bob <bob@coolgroups.com> - 2011-09-04 19:45 -0700
  Re: StringBuilder Arne Vajhøj <arne@vajhoej.dk> - 2011-09-04 22:53 -0400
  Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-05 05:27 +0200
    Re: StringBuilder Arne Vajhøj <arne@vajhoej.dk> - 2011-09-05 19:34 -0400
      Re: StringBuilder Wanja Gayk <brixomatic@yahoo.com> - 2011-09-11 16:35 +0200
        Re: StringBuilder Arne Vajhøj <arne@vajhoej.dk> - 2011-09-11 16:48 -0400
          Re: StringBuilder Wanja Gayk <brixomatic@yahoo.com> - 2011-09-17 02:54 +0200
            Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-17 00:27 -0700
            Re: StringBuilder Lew <lewbloch@gmail.com> - 2011-09-18 01:42 -0700
              Re: StringBuilder Wanja Gayk <brixomatic@yahoo.com> - 2011-09-19 00:59 +0200
                Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-19 05:07 -0700
                Re: StringBuilder Jeff Higgins <jeff@invalid.invalid> - 2011-09-19 11:38 -0400
    Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 17:01 -0700
    Re: StringBuilder Stanimir Stamenkov <s7an10@netscape.net> - 2011-09-17 19:56 +0300
      Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-17 20:35 +0200
      Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-17 15:34 -0700
        Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 01:33 +0200
          Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 01:56 +0200
          Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-17 20:58 -0700
        Re: StringBuilder Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-09-17 22:44 -0700
          Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 09:54 +0200
            Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 09:59 +0200
            Re: StringBuilder Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-09-18 07:28 -0700
          Re: StringBuilder Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-18 08:15 -0400
            Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 15:32 +0200
              Re: StringBuilder Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-18 09:50 -0400
                Re: StringBuilder Stanimir Stamenkov <s7an10@netscape.net> - 2011-09-18 17:08 +0300
                Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 22:13 +0200
                Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 22:29 +0200
                Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 22:39 +0200
          Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-19 09:45 -0700
  Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 11:08 -0700
    Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-05 23:54 +0200
      Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-06 00:06 +0200
      Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 17:03 -0700
        Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-06 08:53 +0200

csiph-web