Re: String and StringBuilder

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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







.



Relevant Pages

  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Secure C library
    ... I read much of the new "security TR", and gee, I don't know. ... the buffer from the buffer size. ... It is not hard to design a better form of buffer and string handling. ... but this is just one example of how thoughtful interface design can ...
    (comp.std.c)
  • Re: Secure C library
    ... >> string functions don't make much sense once you add bounds-checking ... >> designing an interface just for the purpose of reducing the frequency ... > make buffer size decisions more visible, ... Bstrlib is also very interoperable with char *'s, ...
    (comp.std.c)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you check EVERY return from a call that can fail, ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)
  • Re: Calling dll functions from vb.net with pointer returns!
    ... (ByRef pulLen As Integer, ByVal pszFilter As String, ByVal ulFlags As ... OUT PTCHAR Buffer, ... Address of a buffer to receive a set of NULL-terminated device instance ... pszFilter must specify the name of a device ...
    (microsoft.public.dotnet.languages.vb)