Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!k23g2000pri.googlegroups.com!not-for-mail From: lewbloch Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder Difficulties Date: Sun, 3 Jul 2011 00:08:07 -0700 (PDT) Organization: http://groups.google.com Lines: 60 Message-ID: <7c5d329e-6db5-4b52-8501-b9ae9c3a8738@k23g2000pri.googlegroups.com> References: <971fj1Fg1rU1@mid.individual.net> NNTP-Posting-Host: 108.89.33.208 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1309676995 21181 127.0.0.1 (3 Jul 2011 07:09:55 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 3 Jul 2011 07:09:55 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: k23g2000pri.googlegroups.com; posting-host=108.89.33.208; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HNKRUAELSC X-HTTP-UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5829 Gene Wirchenko wrote: > blm...@myrealbox.com wrote >> up a string piece by piece. =A0You *could* write something like >> >> String a =3D "first"; >> a +=3D " second"; >> a +=3D " etc"; >> >> and my *guess* is that this is not horribly inefficient if the >> number of concatenation operations is small. =A0(The conventional > > =A0 =A0 =A0IOW, who cares if it is only a bit of inefficiency? =A0Agreed. > >> wisdom, as I understand it, is that the Java runtime is pretty >> good at managing short-lived objects, so creating new objects is >> not invariably something to avoid, though as with anything else >> one shouldn't get carried away, maybe. =A0Again the experts may >> disagree.) >> >> But if there are a lot of concatenation operations it's said to be >> more efficient to use a StringBuilder, e.g.: >> >> StringBuilder sb =3D new StringBuilder(); >> sb.append("first"); >> sb.append(" second"); >> sb.append(" etc"); >> String a =3D sb.toString(); > > =A0 =A0 =A0It is. =A0My test code appends one character at a time. Switch= ing > from String to StringBuilder cut the execution time by about 40%. > >> The above is what I mostly use StringBuilder for; there are >> undoubtedly other things one can do with it as well, some of >> which may be useful to you (I haven't followed carefully all the >> threads you've started). >> Lines like: String thing =3D getResult() + " found by " + foundSource() +"."; get optimized under the hood, so use of something like 'StringBuilder' on such one-liners is useless. The usefulness kicks in with the sort of idiom blmblm described - multi-line expressions in loops where the compiler builds lots of 'String' instances and can't figure out to use 'StringBuilder' by itself. The efficiency doesn't come from the re-use of the 'StringBuilder' object from iteration to iteration. The create/destroy mechanism for short-lived objects favors creating the temporary object inside the loop, not outside. The efficiency comes from not having to create so many superfluous 'String' objects. -- Lew