Re: String Builder & String, what's the difference ?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Kevin Spencer (kspencer_at_takempis.com)
Date: 06/25/04


Date: Fri, 25 Jun 2004 13:24:21 -0400


> in a few words - stringbuilder is much faster! use it as often as you can
instead

Odd that Microsoft doesn't recommend it that way...

Once a StringBuilder class is instantiated, it is indeed much faster.
However, the instantiation of an Object is costly in itself. A String is a
primitive, which means that unless you treat it as an object, it is
certainly much faster to use than an Object (which is why the .Net framework
includes primitives). In addition, when you instantiate a StringBuilder, it
allocates the default amount of Memory needed to hold its entire buffer (the
buffer is how it avoids re-allocating Memory). Therefore, if you are doing a
small amount of work with a string, it may indeed more efficient NOT to use
a StringBuilder. Consider the following:

StringBuilder s = new StringBuilder("Hello Mom").;
s.Append(", I mean, Mother");
Response.Write(s.ToString());

string s = "Hello Mom";
s += ", I mean, Mother";
Response.Write(s);

Which runs faster? Which uses the most Memory?

-- 
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"ISK" <ISK@discussions.microsoft.com> wrote in message
news:43B1DD35-A3BE-4D3D-A3BD-79D046905B58@microsoft.com...
> in a few words - stringbuilder is much faster! use it as often as you can
instead
> of the regular string especially if you are doing a lot of string
manipulations.
>
> a little more in depth - a string is 'unchangeable' in the sense that once
you've
> assigned a value to it, it never changes; any modifications you make to it
like
> say adding more characters, appending a string or trimming it etc will
create a
> new string object and assign the modified value to it and discard the old
string
> object leaving it for the GC to take care of during its collection
routines..
> so a lot of string manipulation means a lot of allocation de-allocation of
memory
> which isn't very efficient..obviously this also means that the methods
like
> s3 = s1 & s2 are slower than you would expect..
>
> on the other hand, the stringbuilder creates a sort of buffer (default is
128 chars
> but u can change it with its overloaded constructor) and whenever you use
the
> 'append' method, it adds the appended character(s) or string to the
created
> buffer...thus there is no creation-destruction of objects on the fly which
means
> its much faster..
>
> just to give you an idea..the operation s1 = s1 & s2 is almost 100 times
slower
> than stringbuilder1 = stringbuilder1.append(s2) (assuming s1 and
stringbuilder1
> are of same lengths) even for the smallest of lengths of s1 and s2 (run
the
> operations in a loop of say like a hundred thousand to notice the
difference)
>
> hope this helps..
>
> "Tee" wrote:
>
> > String Builder & String, what's the difference.
> > and when to use which ?
> >
> >
> > Thanks.
> >
> >
> >


Relevant Pages

  • Re: Is this string input function safe?
    ... return a pointer to mallocated memory holding one input string, ... complains about use of deallocated pointers, ... mallocating an appropriate amount of memory. ... the contents of the buffer are indeterminate (for different ...
    (comp.lang.c)
  • Re: strange behaviour
    ... Pointer to the buffer to receive the null-terminated string containing ... if the Windows directory is named Windows ... string copied to the buffer, not including the terminating null character. ... You must supply memory for windows to put the directory into. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Buffer or Realloc?
    ... better to allocate memory and realloc it for the size of the what is ... between deciding to use a fixed size buffer or allocating memory ... so for the string I've got to prepare as part of a message to the UK Government gateway where the specification says the string has a maximum length of 10 characters I should not use a fixed size buffer but a reallocating buffer? ... with the realloc() approach -- obviously ...
    (comp.lang.c)
  • Re: Something wrong in my program
    ... what becomes of the memory block starting at this address is no ... our text buffer can contain 15 characters ... a string is a char array *terminated ...
    (comp.lang.c)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)