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


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

Re: StringBuilder

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: StringBuilder
Date 2011-09-18 01:42 -0700
Organization http://groups.google.com
Message-ID <15369681.1051.1316335323980.JavaMail.geo-discussion-forums@prfh23> (permalink)
References (1 earlier) <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> <MPG.28de0e1952218cb9896be@202.177.16.121>

Show all headers | View raw


Wanja Gayk wrote:
> ar...@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?

You said "today's 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.
> 
> 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

That tells part of the story, but extra objects still cause both memory and time overhead.  Time comes from GC cycles that kick in because you were profligate.
...[snip] ...
> 
> 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 

But you changed your argument.  You _were_ speaking of "today's compilers".  Now suddenly you're speaking of "tomorrows [sic] JIT-compilers".

Besides shifting your ground, you have weakened your argument.  It's one thing to program to allow "tomorrow's compilers" to further optimize your code, it's quite another to program to *require* "tomorrow's compilers" to further optimize your code.

You don't know for a fact that "tomorrow's compilers" will ever optimize across multiple lines of 'String' concatenation.

As you say, they "could be" able to do that; you just cannot aver that they will be able to do that.

But of course, that's only now that you've moved the discussion from "today's compilers" as you first said, to "tomorrow's compilers" as you now wish to discuss.

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

That would likely be incorrect anyway, since the semantics differ.  Of course, "tomorrow's JIT compiler" could figure out that there's no thread leak, so it "could be" able to do it.  Maybe.

-- 
Lew

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


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