Which is Faster - Appending to a String or a StringBuilder?

  • Thread starter Thread starter Joe Keller
  • Start date Start date
J

Joe Keller

Hello,

For appending characters onto an existing set of characters, is it faster to
use a String object or the StringBuilder object?

For example, is there any performance difference between the two or is one
more appropriate and compact than another:

Example #1
string s = "testing";
s += "testing";

Example #2
StringBuilder sb = new StringBuilder();
sb.Append("testing");
sb.Append("testing");

Thanks!

Joe
 
Hi Joe:

Strings are immutable and in general it's preferable to use the
stringbuilder. I don't have any of the sample tests in front of me, but I
remember seeing on that I believe was done by Dan Appleman. In general a
stringbuilder is more efficient but that efficiency is realized in
proportion to how much concatenation is being done. So if you did 50
concatenations, you'd see a more profound improvement with the stringbuilder
than say one small one like your example. The larger and more
concatenations, the more you should go with a stringbuilder. You should
also intialize the stringbuilder to the approximate size that you expect the
ultimate string to be b/c this can further optimize it. I believe (and my
memory is foggy on this) it was like 15 characters as the default and each
time that's exceeded it has to redimension itself although now that I think
about it, that seems a bit small. But even if you don't use a dimension at
the onset, the stringbuilder is going to be faster.

HOWEVER, for small scenarios, like the one you mention explicitly below, the
string may in fact be more efficient b/c of the overhead assocaited with
managing the stringbuilder. What's the magic number where it crosses over?
I don't know and as I remember the articles I've read, I know they all
mentioned that there wasn't a magic number but it was like 5 or so
concatenations where the difference would start showing itself. The size of
what you are concatenating obviously had an effect but if you are doing more
than 1 or < 5, I wouldn't worry too much about it. After that you
definitely want to use the stringbuilder and as a rule, I use them unless
it's just one modification.

The main thing to remember is that a string is immutable so if you declare
string s = "bill";
and now you do s+=" ryan";
a new string is created w a reference to the first one but the actual
variable s doesn't remain unchanged.

I'll see if I can find those tests b/c I know my answer was ambiguous as to
where you should start distinguishing the difference.
http://dotnetjunkies.com/WebLog/donnymack/archive/2003/07/18/475.aspx
http://www.thecodeproject.com/Purgatory/string.asp


HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
Helps a lot - thanks!

Joe

William Ryan eMVP said:
Hi Joe:

Strings are immutable and in general it's preferable to use the
stringbuilder. I don't have any of the sample tests in front of me, but I
remember seeing on that I believe was done by Dan Appleman. In general a
stringbuilder is more efficient but that efficiency is realized in
proportion to how much concatenation is being done. So if you did 50
concatenations, you'd see a more profound improvement with the
stringbuilder
than say one small one like your example. The larger and more
concatenations, the more you should go with a stringbuilder. You should
also intialize the stringbuilder to the approximate size that you expect
the
ultimate string to be b/c this can further optimize it. I believe (and my
memory is foggy on this) it was like 15 characters as the default and each
time that's exceeded it has to redimension itself although now that I
think
about it, that seems a bit small. But even if you don't use a dimension
at
the onset, the stringbuilder is going to be faster.

HOWEVER, for small scenarios, like the one you mention explicitly below,
the
string may in fact be more efficient b/c of the overhead assocaited with
managing the stringbuilder. What's the magic number where it crosses
over?
I don't know and as I remember the articles I've read, I know they all
mentioned that there wasn't a magic number but it was like 5 or so
concatenations where the difference would start showing itself. The size
of
what you are concatenating obviously had an effect but if you are doing
more
than 1 or < 5, I wouldn't worry too much about it. After that you
definitely want to use the stringbuilder and as a rule, I use them unless
it's just one modification.

The main thing to remember is that a string is immutable so if you declare
string s = "bill";
and now you do s+=" ryan";
a new string is created w a reference to the first one but the actual
variable s doesn't remain unchanged.

I'll see if I can find those tests b/c I know my answer was ambiguous as
to
where you should start distinguishing the difference.
http://dotnetjunkies.com/WebLog/donnymack/archive/2003/07/18/475.aspx
http://www.thecodeproject.com/Purgatory/string.asp


HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
Back
Top