StringBuilder vs. Manually Building Strings

J

Jason Cavett

Alright, I realize that manually building strings via the String
concatenation operator (AKA: string3 = string1 + string2) is a very
expensive operation due to how Java handles it. As far as I know,
during String concatenation, Java creates a StringBuffer, appends the
two arguments together and the calls toString() on the StringBuffer to
return the String.

So, my question is - would it be better for me to use StringBuilder
most of the time? It seems like it would be in all cases where I'm
doing quite a bit of concatenation. However, for a single
concatenation, I'm not sure this is going to be any kind of
improvement.

Thanks for any insight.
 
K

Knute Johnson

Jason said:
Alright, I realize that manually building strings via the String
concatenation operator (AKA: string3 = string1 + string2) is a very
expensive operation due to how Java handles it. As far as I know,
during String concatenation, Java creates a StringBuffer, appends the
two arguments together and the calls toString() on the StringBuffer to
return the String.

So, my question is - would it be better for me to use StringBuilder
most of the time? It seems like it would be in all cases where I'm
doing quite a bit of concatenation. However, for a single
concatenation, I'm not sure this is going to be any kind of
improvement.

Thanks for any insight.

Anytime you can do the concatenation in the compile it will be the
quickest solution. If your concatenation is occurring at runtime then
the StringBuilder will be fastest.

But I think your real question is what is the performance hit for the
occasional concatenation with the + operator compared to writing more
complicated code. In most cases I think the answer is microseconds and
well worth the trade off to make it easier to code and read.
 
A

Arne Vajhøj

Jason said:
Alright, I realize that manually building strings via the String
concatenation operator (AKA: string3 = string1 + string2) is a very
expensive operation due to how Java handles it. As far as I know,
during String concatenation, Java creates a StringBuffer, appends the
two arguments together and the calls toString() on the StringBuffer to
return the String.

So, my question is - would it be better for me to use StringBuilder
most of the time? It seems like it would be in all cases where I'm
doing quite a bit of concatenation. However, for a single
concatenation, I'm not sure this is going to be any kind of
improvement.

The String concatenation versus StringBuffer/StringBuilder is usually
first example in all Java performance books/articles. And
due to that all Java programmers are very concerned about it.

If you need to concatenate in loop with many iterations,
then using StringBuffer/StringBuilder is a good thing.

But in many other cases, then forget it and write some simple
code with + operator.

Arne
 
R

Roedy Green

So, my question is - would it be better for me to use StringBuilder
most of the time? It seems like it would be in all cases where I'm
doing quite a bit of concatenation.

the + notation is terser and easier to maintain. You might as well
use it when it will generate the same code anyway e.g. an expression
of the form

a =b + c + d + e;

The only advantage of StringBuilder there is you can provide a better
guess for the buffer size.

Code of this form:

a += b;
a +=c;
if ( x )
{
a += d;
}

will probably be better written with StringBuilder even if just to
make it clearer to newbies reading your code what you are doing.

IntelliJ has a feature where you can interconvert the various ways of
building Strings with just a click. So you can write it with + and
flip it to StringBuilder.
 
R

Roedy Green

String remarkByArthur =
"This is obviously some strange usage of the word "
+ theWord
+ " that I hadn't previously been aware of.";

I believe, since the pieces are all literals, the concatenation will
be done at compile time, making it faster than the StringBuilder
approach.
 
A

Arne Vajhøj

Roedy said:
I believe, since the pieces are all literals, the concatenation will
be done at compile time, making it faster than the StringBuilder
approach.

theWord looks like a variable to me !

Arne
 
K

Knute Johnson

Arne said:
theWord looks like a variable to me !

Arne

In the example below the + concatenation is done by the compiler at
compile time. In this case on my computer it takes something less than
16ms to do the + and over 9 seconds to do the StringBuilder.append().
However if you are going to concatenate a string to the same string I'm
sure this loop would be much longer.

public class test7 {
public static void main(String[] args) {
long now = System.currentTimeMillis();
for (int i=0; i<10000000; i++) {
String str = "lkasjdf l;asjdff kasdjf lkajsdd askdf " +
";lkajsdf lals;kjdflkjas dfjla;sjdw09023r09wefqweo" +
"0909f0q98409asd 029f09asdf02e9f8 0asdf09wqef qwe";
}
long then = System.currentTimeMillis();
System.out.println("+ " + Long.toString(then - now));

now = System.currentTimeMillis();
for (int i=0; i<10000000; i++) {
StringBuilder sb = new StringBuilder(200);
sb.append("lkasjdf l;asjdff kasdjf lkajsdd askdf ");
sb.append(
";lkajsdf lals;kjdflkjas dfjla;sjdw09023r09wefqweo");
sb.append(
"0909f0q98409asd 029f09asdf02e9f8 0asdf09wqef qwe");
String str = sb.toString();
}
then = System.currentTimeMillis();
System.out.println("SB " + Long.toString(then - now));
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top