Re: String Builder & String, what's the difference ?
From: Kevin Spencer (kspencer_at_takempis.com)
Date: 06/25/04
- Next message: Kevin Spencer: "Re: Simple Simple question!!!"
- Previous message: ashelley_at_inlandkwpp.com: "Re: Newbie - Problem Sending an email via .NET"
- In reply to: ISK: "RE: String Builder & String, what's the difference ?"
- Next in thread: Tom Shelton: "Re: String Builder & String, what's the difference ?"
- Reply: Tom Shelton: "Re: String Builder & String, what's the difference ?"
- Messages sorted by: [ date ] [ thread ]
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. > > > > > >
- Next message: Kevin Spencer: "Re: Simple Simple question!!!"
- Previous message: ashelley_at_inlandkwpp.com: "Re: Newbie - Problem Sending an email via .NET"
- In reply to: ISK: "RE: String Builder & String, what's the difference ?"
- Next in thread: Tom Shelton: "Re: String Builder & String, what's the difference ?"
- Reply: Tom Shelton: "Re: String Builder & String, what's the difference ?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|