PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework Which is Faster - Appending to a String or a StringBuilder?

Reply

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

 
Thread Tools Rate Thread
Old 28-05-2004, 01:33 AM   #1
Joe Keller
Guest
 
Posts: n/a
Default Which is Faster - Appending to a String or a StringBuilder?


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


  Reply With Quote
Old 28-05-2004, 02:51 AM   #2
William Ryan eMVP
Guest
 
Posts: n/a
Default Re: Which is Faster - Appending to a String or a StringBuilder?

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/don.../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/
"Joe Keller" <josepk@hotmail.com> wrote in message
newsoKdnQYkxcd4FSvdRVn-ig@comcast.com...
> 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
>
>



  Reply With Quote
Old 28-05-2004, 04:13 PM   #3
Joe Keller
Guest
 
Posts: n/a
Default Re: Which is Faster - Appending to a String or a StringBuilder?

Helps a lot - thanks!

Joe

"William Ryan eMVP" <dotnetguru@comcast.nospam.net> wrote in message
news:%23ieJPZFREHA.2032@TK2MSFTNGP11.phx.gbl...
> 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/don.../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/
> "Joe Keller" <josepk@hotmail.com> wrote in message
> newsoKdnQYkxcd4FSvdRVn-ig@comcast.com...
>> 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
>>
>>

>
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off