|
From: Roedy Green on 19 Jul 2008 14:33 I put in a little bit of code like this to see how good I was at estimating the initial size for StringBuilder size allocation. I was embarrassed to discover I badly underestimated in every single case. That meant StringBuilder had to pause in the middle of each string constructed to double the buffer size, then garbage collect twice as many objects as it need have done. You might use this code to check out how good you are at estimating. /** * Used to fine tune initial StringBuilder size estimates. Insert a call to checkEstimate just before toString. * * @param sb the StringBuilder to check. * @param initSize initial size the StringBuilder was allocated. */ static void checkEstimate( StringBuilder sb, int initSize ) { final int size = sb.length(); if ( size > initSize ) { Throwable t = new Throwable(); StackTraceElement[] es = t.getStackTrace(); StackTraceElement e = es[ 1 ]; err.println( "at " + e.getClassName() + "." + e.getMethodName() + " line:" + e.getLineNumber() ); err.println( "StringBuffer initially sized too small " + initSize + " to contain " + size + " without autogrowing." ); } else { if ( size + 100 < initSize ) { Throwable t = new Throwable(); StackTraceElement[] es = t.getStackTrace(); StackTraceElement e = es[ 1 ]; err.println( "at " + e.getClassName() + "." + e.getMethodName() + " line:" + e.getLineNumber() ); err.println( "StringBuffer initially sized needlessly large " + initSize + " to contain contain " + size ); } } } -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
From: Kevin McMurtrie on 20 Jul 2008 03:34 In article <blc484dh1e53u0mqf48lsc2igfb941p50h(a)4ax.com>, Roedy Green <see_website(a)mindprod.com.invalid> wrote: > I put in a little bit of code like this to see how good I was at > estimating the initial size for StringBuilder size allocation. > > I was embarrassed to discover I badly underestimated in every single > case. That meant StringBuilder had to pause in the middle of each > string constructed to double the buffer size, then garbage collect > twice as many objects as it need have done. > > You might use this code to check out how good you are at estimating. > > > /** > * Used to fine tune initial StringBuilder size estimates. Insert > a call to checkEstimate just before toString. > * > * @param sb the StringBuilder to check. > * @param initSize initial size the StringBuilder was allocated. > */ > static void checkEstimate( StringBuilder sb, int initSize ) > { > final int size = sb.length(); > if ( size > initSize ) > { > Throwable t = new Throwable(); > StackTraceElement[] es = t.getStackTrace(); > StackTraceElement e = es[ 1 ]; > err.println( "at " + e.getClassName() > + "." + e.getMethodName() > + " line:" + e.getLineNumber() ); > err.println( "StringBuffer initially sized too small " + > initSize + " to contain " + size + " without autogrowing." ); > } > else > { > if ( size + 100 < initSize ) > { > Throwable t = new Throwable(); > StackTraceElement[] es = t.getStackTrace(); > StackTraceElement e = es[ 1 ]; > err.println( "at " + e.getClassName() > + "." + e.getMethodName() > + " line:" + e.getLineNumber() ); > err.println( "StringBuffer initially sized needlessly > large " + initSize + " to contain contain " + size ); > } > } > } Or put a breakpoint on AbstractStringBuilder.expandCapacity(). -- Goolge is a pro-spamming service. I will not see your reply if you use Google.
From: Roedy Green on 20 Jul 2008 09:49 On Sat, 19 Jul 2008 18:33:35 GMT, Roedy Green <see_website(a)mindprod.com.invalid> wrote, quoted or indirectly quoted someone who said : > /** > * Used to fine tune initial StringBuilder size estimates. Insert >a call to checkEstimate just before toString. > * I have posted a somewhat improved version at http://mindprod.com/jgloss/stringbuilder.html Getting the code so it generates no warnings is pretty quick. The key to it is sorting the warning messages, so you can see what sort of typical sizes of result there are at a glance. I have a huge amounts of code that basically uses StringBuilder to build strings that are then cascaded to build even bigger strings. I grossly overestimated by ability to by eye generate a good estimate. This little optimisation doubles my RAM efficiency -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
From: Mike Schilling on 20 Jul 2008 12:24 Roedy Green wrote: > On Sat, 19 Jul 2008 18:33:35 GMT, Roedy Green > <see_website(a)mindprod.com.invalid> wrote, quoted or indirectly > quoted > someone who said : > >> /** >> * Used to fine tune initial StringBuilder size estimates. >> Insert >> a call to checkEstimate just before toString. >> * > > I have posted a somewhat improved version at > http://mindprod.com/jgloss/stringbuilder.html > > Getting the code so it generates no warnings is pretty quick. The > key > to it is sorting the warning messages, so you can see what sort of > typical sizes of result there are at a glance. > > I have a huge amounts of code that basically uses StringBuilder to > build strings that are then cascaded to build even bigger strings. > > I grossly overestimated by ability to by eye generate a good > estimate. > This little optimisation doubles my RAM efficiency When you measure things like elapsed time or CPU usage in the entire application, how much difference does it make?
From: Roedy Green on 21 Jul 2008 06:58 On Sun, 20 Jul 2008 09:24:23 -0700, "Mike Schilling" <mscottschilling(a)hotmail.com> wrote, quoted or indirectly quoted someone who said : >When you measure things like elapsed time or CPU usage in the entire >application, how much difference does it make? I stupidly did not benchmark before the changes. -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
|
Next
|
Last
Pages: 1 2 3 Prev: Optimal path engine project, kook-puppets needed Next: What I learned from Class Viewer |