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


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

Re: StringBuilder

From Wanja Gayk <brixomatic@yahoo.com>
Newsgroups comp.lang.java.programmer
Subject Re: StringBuilder
Date 2011-09-17 02:54 +0200
Organization Netfront http://www.netfront.net/
Message-ID <MPG.28de0e1952218cb9896be@202.177.16.121> (permalink)
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> <4e6d1eb6$0$306$14726298@news.sunsite.dk>

Show all headers | View raw


In article <4e6d1eb6$0$306$14726298@news.sunsite.dk>, arne@vajhoej.dk 
says...

> >> 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.

And "still" is the keyword. The question remains: For how long will this 
hold true?

> 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.

So what's the problem with these? before answering, just have a look at 
this: 
http://www.ibm.com/developerworks/java/library/j-jtp09275/index.html

> > 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 +.

Nope, I'm arguing that:

String x="";
for(String s : strings){
 x+=s;
}

or 

String x="";
for(String s : strings){
 x=x+s;
}

Is simple pattern that could be detected by tomorrows JIT-compilers and 
transformed into:

StringBuilder b = new StringBuilder();
for(String s : strings){
 b.append(s);
}
String x = b.toString();

But I don't think any compiler programmer would care to detect this 
pattern:

StringBuffer b = new StringBuffer ();
for(String s : strings){
 b.append(s);
}
String x = b.toString();

Just to transform it into the the StringBuilder-variant.

> > 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.

That's exactly what I'm saying.

Kind regards,
Wanja


-- 
..Alesi's problem was that the back of the car was jumping up and down 
dangerously - and I can assure you from having been teammate to 
Jean Alesi and knowing what kind of cars that he can pull up with, 
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]

--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

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


Thread

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

csiph-web