Re: String and StringBuilder
- From: "Greg Young" <druckdruckREMOVEgoose@xxxxxxxxxxx>
- Date: Mon, 31 Jul 2006 15:06:11 -0400
There is a break even point when it is passed the string may actually be
slightly faster than the stringbuilder (these are generally very small
numbers like 6 (and when being compared to s tringbuilder without an initial
size as it has to grow its internal buffer which equates to almost exactly
what is happenning in the case of string)
Generally I tell people to stay away from this code unless they are _really_
optimizing an area as often times requirements will change slightly and the
stringbuilder will become faster.
Cheers,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"Harvey Triana" <harveytriana@xxxxxxxxxxx> wrote in message
news:%23eiEX%23MtGHA.2260@xxxxxxxxxxxxxxxxxxxxxxx
Greg, a question...
When loop times is low, say
for(int i=0;i<8;i++) {
b+="hello";
}
should use the class StringBuilder? ... also
<HT />
"Greg Young" <druckdruckREMOVEgoose@xxxxxxxxxxx> escribió en el mensaje
news:eghmz4MtGHA.4324@xxxxxxxxxxxxxxxxxxxxxxx
In the background? Not sure I am understanding you here.
The reason StringBuilder is generally more performant is because strings
are immutable.
string b = "";
for(int i=0;i<100;i++) {
b+="hello";
}
return b;
vs
StringBuilder b = new StringBuilder(InitialSize);
for(int i=0;i<100;i++) {
b.Append("hello");
}
return b.ToString();
each of the operations in the first loop create a new string as strings
are immutable (they cannot (well atleast in the safe world) be
changed)... the string builder continues using the same buffer. Since the
string builder uses the same object over and over (alterring an internal
buffer) it will use far less memory than the string example. The major
speed gain is realized in garbage collection and in the copying of memory
...
Creating objects in .NET is very fast, it is getting rid of them that is
slow .. since the first example creates so many intermediate objects the
garbage collector has alot of work to do to get rid of them.
For copying of memory the string example is basically copying the data
for the entire size of the string everytime a new string is created (for
small data sets this is rather minor but for big strings it gets quite
expensive). The StringBuilder class will also copy data if it has to
expand its buffer but will generally only have to copy the data that is
actually being appended (as opposed to all of the data).
Cheers,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"Hardy Wang" <hardywang@xxxxxxxxxxxxxxxxx> wrote in message
news:%23nSJ7wMtGHA.3684@xxxxxxxxxxxxxxxxxxxxxxx
Hi all,
I know it is better to handle large string with a StringBuilder, but
how does StringBuilder class improve the performance in the background?
Thanks!
--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
.
- References:
- String and StringBuilder
- From: Hardy Wang
- Re: String and StringBuilder
- From: Greg Young
- Re: String and StringBuilder
- From: Harvey Triana
- String and StringBuilder
- Prev by Date: TreeView Component in visual Studio 2005
- Next by Date: Re: Making A Constructor Only Available To Another Class that Doesn't Inherit From It
- Previous by thread: Re: String and StringBuilder
- Next by thread: Re: String and StringBuilder
- Index(es):
Relevant Pages
|