• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuffer performance

 
harold christensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a large text processing application, and for
once I am worried about java performance. Can someone
help me evaluate these two approaches to meet the
principle:
Reuse; do not destroy and re construct in a loop.
Which scenario improves performance the most, and why?
Here is a terse example:
StringBuffer sb = new StringBuffer();
//...
While (loopIsNotDone) {
//...
// which of these two is best?
// sb.delete(0, sb.length()); // #1 would use this!
sb = null; // #2 two uses this!
doTheChore(sb);
saveResult(sb.toString());
//...
}

------------------
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here are some articles that may help
http://developer.java.sun.com/developer/JDCTechTips/2001/tt0410.html#optimizing
Some other tips from http://www.javaperformancetuning.com/tips.shtml:
- Use StringBuffer rather than the string concatenation operator (+).
- Use char arrays for all character processing in loops, rather than using the String or StringBuffer classes.
- Set the initial StringBuffer size to the maximum string length, if it is known.
- Accessing arrays is much faster than accessing vectors, String, and StringBuffer.
- Avoid using String when doing a lot of character manipulation. Use StringBuffer instead.
- Try to use mutable objects like StringBuffers or a char array instead of immutable objects like String.
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!