Thread: StringBuffer vs. StringBuilder

StringBuffer vs. StringBuilder

From
Frederik Wiers
Date:
Hello,

suggestion for a small performance improvement: replace StringBuffer with StringBuilder when the StringBuffer is used as a local variable in a method.

I just browsed the git-repo (https://github.com/pgjdbc/pgjdbc/tree/master/org/postgresql) looking for prepared statement caching (which I could not find, but that is for another day) and saw a lot of places where StringBuffer can be replaced by StringBuilder.

The little performance test shown at http://stackoverflow.com/a/2771852/3080094
indicates that if StringBuffer is used a lot, replacing it with StringBuilder can make a difference. And I can not think of a reason to use a StringBuffer if it is just used locally in a method to build a String.

Frederik


Re: StringBuffer vs. StringBuilder

From
Stephen Nelson
Date:
On Mon, Jan 20, 2014 at 12:37 AM, Frederik Wiers <frederik.wiers@gmail.com> wrote:
And I can not think of a reason to use a StringBuffer if it is just used locally in a method to build a String.


It's probably historical based on the fact that StringBuilder was introduced in JDK 1.5 and pgjdbc is/was still supporting JDK 1.4. It's worth re-evaluating if 1.4 should be supported and then a number of optimisations could be introduced.

Re: StringBuffer vs. StringBuilder

From
Dave Cramer
Date:
Frederik,

Statement caching has been a fairly hot topic of late. What would you expect statement caching to give you ?

Currently we don't really parse the statements. So parsing isn't a huge overhead, but there is some.
We don't use a named statement on the server so that is relatively quick.

I'd be interested in seeing a proof of concept with some performance numbers if you have some cycles?

Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca


On Sun, Jan 19, 2014 at 7:37 PM, Frederik Wiers <frederik.wiers@gmail.com> wrote:
Hello,

suggestion for a small performance improvement: replace StringBuffer with StringBuilder when the StringBuffer is used as a local variable in a method.

I just browsed the git-repo (https://github.com/pgjdbc/pgjdbc/tree/master/org/postgresql) looking for prepared statement caching (which I could not find, but that is for another day) and saw a lot of places where StringBuffer can be replaced by StringBuilder.

The little performance test shown at http://stackoverflow.com/a/2771852/3080094
indicates that if StringBuffer is used a lot, replacing it with StringBuilder can make a difference. And I can not think of a reason to use a StringBuffer if it is just used locally in a method to build a String.

Frederik