Google Groups Home
Help | Sign in
StringBuilder Performance vs. String Concatenation
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 31 - Collapse all   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Kevin C  
View profile
 More options Oct 16 2003, 10:36 am
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: "Kevin C" <kevin-calla...@sbcglobal.net>
Date: Thu, 16 Oct 2003 14:35:16 GMT
Local: Thurs, Oct 16 2003 10:35 am
Subject: StringBuilder Performance vs. String Concatenation
Quick Question:

StringBuilder is obviously more efficient dealing with string concatenations
than the old '+=' method... however, in dealing with relatively large string
concatenations (ie, 20-30k), what are the performance differences (if any
with something as trivial as this) between initializing a new instance of
StringBuilder with a specified capacity vs. initializing a new instance
without... (the final length is not fixed)

ie,

Performance differences between:

StringBuilder sb1 = new StringBuilder(30000);

and

StringBuilder sb1 = new StringBuilder();

Does this make a huge difference compared to '+='?

Cheers,
-k


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William Ryan  
View profile
 More options Oct 16 2003, 10:48 am
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: "William Ryan" <dotnetg...@comcast.nospam.net>
Date: Thu, 16 Oct 2003 10:49:15 -0400
Local: Thurs, Oct 16 2003 10:49 am
Subject: Re: StringBuilder Performance vs. String Concatenation
IF you are doing large string concatenations, definitely use the
StringBuilder.  There's no magic number per se, but on trivial
concatentions, it's not a big deal.  The main thing to remember is that
strings are immutable, so 50 concatenations creates 50 string objects.

If you initialize the SB, you are better off if you go over the default size
because it doesn't have to reallocate space, but this only comes into play
if you exceed the default size.  The performance differences really only are
noticed between the two if you exceed the boundaries, so it's hard to say in
absolute terms, it depends on the situation.  If possible, try to initialize
the capacity, but even if you don't, you'll be much better off than +=.

HTH,

Bill

"Kevin C" <kevin-calla...@sbcglobal.net> wrote in message

news:E0yjb.1401$FB1.981@newssvr27.news.prodigy.com...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin C  
View profile
 More options Oct 16 2003, 11:15 am
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: "Kevin C" <kevin-calla...@sbcglobal.net>
Date: Thu, 16 Oct 2003 15:14:51 GMT
Local: Thurs, Oct 16 2003 11:14 am
Subject: Re: StringBuilder Performance vs. String Concatenation
bill,

thanks for the quick reply -- that makes sense.

As a footnote though, which has the most negative (theorical?) effect on
performance:

1) over initializing an instance, ie., setting capacity at 30,000 characters
when you only need 20,000
or
2) under initialzing an instance, ie., setting capacity at 10,000 characters
and having the StringBuilder class dynamically allocate more room for the
additional 10,000 characters when you try to append 20,000.

-k

"William Ryan" <dotnetg...@comcast.nospam.net> wrote in message

news:%23bA$LR$kDHA.2068@TK2MSFTNGP09.phx.gbl...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Dunaway  
View profile
 More options Oct 16 2003, 1:00 pm
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: Chris Dunaway <dunaw...@lunchmeatsbcglobal.net>
Date: Thu, 16 Oct 2003 09:57:42 -0700
Local: Thurs, Oct 16 2003 12:57 pm
Subject: Re: StringBuilder Performance vs. String Concatenation
"Kevin C" <kevin-calla...@sbcglobal.net> wrote in
news:LByjb.1406$UN1.1341@newssvr27.news.prodigy.com:

> As a footnote though, which has the most negative (theorical?) effect
> on performance:

> 1) over initializing an instance, ie., setting capacity at 30,000
> characters when you only need 20,000
> or
> 2) under initialzing an instance, ie., setting capacity at 10,000
> characters and having the StringBuilder class dynamically allocate
> more room for the additional 10,000 characters when you try to append
> 20,000.

I would say that number 2 has the most negative impact.  When you append
characters that exceed the capacity of the StringBuilder, it must allocate
memory large enough to hold the new string, copy the existing characters to
it and then add the new characters.  Whereas on number, the allocation is
already do and it just has to append the new data.

Chris


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jerry III  
View profile
 More options Oct 16 2003, 1:23 pm
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: "Jerry III" <jerry...@hotmail.com>
Date: Thu, 16 Oct 2003 10:21:28 -0700
Local: Thurs, Oct 16 2003 1:21 pm
Subject: Re: StringBuilder Performance vs. String Concatenation
Chris, StringBuilder does not keep the string data in a single continuous
block of memory. What you described (allocating new chunk of memory, copying
old data into it and appending the new data) is exactly how += for strings
work. StringBuilder does not do that, it just allocates new memory to hold
the new data and keeps both the old and the new, but not together. Of course
calling ToString is going to finally add all the small chunks into one
piece, so it only pays off if you do a lot (I've seen posts that roughly 5
was the magic number) of appending.

Jerry

"Chris Dunaway" <dunaw...@lunchmeatsbcglobal.net> wrote in message

news:Xns941679AE7B6A8dunawaycsbcglobalnet@207.46.248.16...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jon Skeet [C# MVP]  
View profile
 More options Oct 16 2003, 2:13 pm
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: Jon Skeet [C# MVP] <sk...@pobox.com>
Date: Thu, 16 Oct 2003 19:11:16 +0100
Local: Thurs, Oct 16 2003 2:11 pm
Subject: Re: StringBuilder Performance vs. String Concatenation

Jerry III <jerry...@hotmail.com> wrote:
> Chris, StringBuilder does not keep the string data in a single continuous
> block of memory. What you described (allocating new chunk of memory, copying
> old data into it and appending the new data) is exactly how += for strings
> work. StringBuilder does not do that, it just allocates new memory to hold
> the new data and keeps both the old and the new, but not together. Of course
> calling ToString is going to finally add all the small chunks into one
> piece, so it only pays off if you do a lot (I've seen posts that roughly 5
> was the magic number) of appending.

Do you have any evidence of this? This is certainly the first I've
heard of it. As far as I'm aware, StringBuilder has a buffer, and once
that is full, the buffer is copied and resized. That's the view that
the rotor source suggests, too.

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephen Martin  
View profile
 More options Oct 16 2003, 2:20 pm
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: "Stephen Martin" <smar...@removethis.emsoft.andthis.ca>
Date: Thu, 16 Oct 2003 14:16:36 -0400
Local: Thurs, Oct 16 2003 2:16 pm
Subject: Re: StringBuilder Performance vs. String Concatenation
Are you sure about that? A quick look at the StringBuilder class with
Anakrino and ildasm seems to show a new allocation and copy of the old
buffer.

"Jerry III" <jerry...@hotmail.com> wrote in message

news:%23ta9vnAlDHA.3700@TK2MSFTNGP11.phx.gbl...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fergus Cooney  
View profile
 More options Oct 16 2003, 3:00 pm
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: "Fergus Cooney" <filte...@tesco.net>
Date: Thu, 16 Oct 2003 19:55:52 +0100
Local: Thurs, Oct 16 2003 2:55 pm
Subject: Re: StringBuilder Performance vs. String Concatenation
Hi Kevin,

    For more insights on Strings:

        Strings UNDOCUMENTED
        http://www.codeproject.com/dotnet/strings.asp

Regards,
Fergus


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fergus Cooney  
View profile
 More options Oct 16 2003, 3:05 pm
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: "Fergus Cooney" <filte...@tesco.net>
Date: Thu, 16 Oct 2003 20:02:24 +0100
Local: Thurs, Oct 16 2003 3:02 pm
Subject: Re: StringBuilder Performance vs. String Concatenation
Hi Folks,

00004 //    Copyright (c) 2002 Microsoft Corporation.  All rights reserved.
00005 //
00017 ** Class:  StringBuilder
00020 **
00021 ** Purpose: A prototype implementation of the StringBuilder
00022 ** class.
00023 **
00024 ** Date:  December 8, 1997
00025 ** Last Updated:  March 31, 1998
00026 **
00028 namespace System.Text {
00029     using System.Text;
00030     using System.Runtime.Serialization;
00031     using System;
00032     using System.Runtime.CompilerServices;
00033
00052     [Serializable()] public sealed class StringBuilder {

Full details at:
http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/stringbui...
source.html

Regards,
Fergus


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jerry III  
View profile
 More options Oct 17 2003, 4:56 am
Newsgroups: microsoft.public.dotnet.framework.performance, microsoft.public.dotnet.framework, microsoft.public.dotnet.general
From: "Jerry III" <jerry...@hotmail.com>
Date: Fri, 17 Oct 2003 01:55:05 -0700
Local: Fri, Oct 17 2003 4:55 am
Subject: Re: StringBuilder Performance vs. String Concatenation
Ok, I was not right, I guess I should decompile before I post something. I'm
really disappointed, I thought the StringBuilder was a lot more efficient
than this, I can just allocate large enough String and get a lot better
performance (memory is pretty cheap).

Jerry

"Fergus Cooney" <filte...@tesco.net> wrote in message

news:u$ZAThBlDHA.1408@TK2MSFTNGP11.phx.gbl...

http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/stringbui...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.