Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.46.80.MISMATCH!postnews.google.com!b39g2000prd.googlegroups.com!not-for-mail From: lewbloch Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder Difficulties Date: Sun, 3 Jul 2011 00:11:10 -0700 (PDT) Organization: http://groups.google.com Lines: 30 Message-ID: <766c1cb2-1fa1-426b-bec8-8069130cc26d@b39g2000prd.googlegroups.com> References: <9v7n071ns1n0plisods3m2pi6q68rtket5@4ax.com> <90mn07h4ul4g3ppijpku1uc3iocc4a7pl0@4ax.com> 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 1309677176 22737 127.0.0.1 (3 Jul 2011 07:12:56 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 3 Jul 2011 07:12:56 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: b39g2000prd.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:5830 On Jun 29, 6:58=A0pm, Gene Wirchenko wrote: > On Wed, 29 Jun 2011 15:52:46 -0700, Patricia Shanahan > wrote: > > [snip] > > >It is relatively rare to decide, part way through a build, to throw away > >the work so far and begin again with entirely different contents. > > =A0 =A0 =A0I am going to be doing it in a loop. =A0why create a new objec= t > each iteration? > To avoid promotion of the object to the tenured generation, which makes GC stuff far less efficient. Quite often it's better to create the temporary object inside the loop. You should do what makes sense for the logic of the algorithm and not try to second-guess and micromanage optimization issues. You totally have your head in the wrong place here. If the scope of the object, say the 'StringBuilder', is inside the loop, declare it inside the loop. Only declare it outside the loop if it's needed outside the loop. Don't try to be a human HotSpot engine, and stop trying to defeat the GC optimizations. -- Lew