String Builder & String, what's the difference ?

D

Donald E. Hurley Jr.

Strings are immutable. Meaning if you try to: string1 = string1 + string2,
it needs to create a new memory location for string1 and set the original
location for GC.

StringBuilder does not behave this way.

Don
 
S

Saul

Tee,

Here's my take on it :

Use stringbuilder whenever you need to do LOTS of string manipulations. For
quick/little things, I don't bother. So if I'm just adding a string in one
line I would do something like this --

strA = strB & strC

However, I had a loop where I was doing about 1,200 concatinations. Using
string builder reduced the time this took substantially.

Others may feel different, but this works for me.

-Saul
 
K

Kevin Spencer

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.
 
T

Tom Shelton

instead

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

They do.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftips.asp

see the section: Use StringBuilder for Complex String Manipulation
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

Not true. Strings are reference types (not "primitives). The only reason
a string MAY be faster to instantiate is because it doesn't allocate a
buffer, it simply allocates a 4-byte pointer and sets it to null. Where as
a stringbuilder will allocate an initial buffer (though the default buffer
size is only sixteen characters).
certainly much faster to use than an Object (which is why the .Net framework
includes primitives).

Strings are objects.
In addition, when you instantiate a StringBuilder, it
allocates the default amount of Memory needed to hold its entire buffer (the

The initial buffer (unless specified) is only sixteen characters...
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?

For this example, a stringbuilder would be overkill. But, put it in a loop
or do more then 15 or 20 appends on the string... StringBuilder will
probably come out the winner in both speed and memory - because with a
regular string, there are temporary string objects being created in the
background when you concatenate.
 
K

Kevin Spencer

Tom, Tom, Tom. Note the exception that I made. You stated (and I quoted, and
now quote):

By your own admission, my example proves this statement to be false:
For this example, a stringbuilder would be overkill.

I'm not into debate. Just trying to clear the air.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
C

Cor Ligthert

Hi Tom,

I was always using
dim sb as new stringbuilder
sb.append("Hello")
sb.append(" how ")
sb.append("are you?")

Than Fergus ask me once, why you do that, this is overkill? I tested it,
and the difference was almost nothing, and therefore I stopped to show it
this way forever (with long strings I always show/do it like above) and did
it with shortstrings as above no more. Then some weeks ago, Jay B stated to
me, that it was faster to do it as above. (You understand it probably, the
fire was on).

To be sure I tested it again in a very long loop, making a long string from
it and making this as short strings (however 100000 times or so). It is
always faster than concatenating the string, so I use this above again.

:)

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
it with shortstrings as above no more. Then some weeks ago, Jay B stated to
me, that it was faster to do it as above. (You understand it probably, the
fire was on).
Please do not take things I say out of context!

I'm not sure which thread you think I told you that, but its obvious to me
you are mis-quoting me!

Thanks
Jay
 
C

Cor Ligthert

Hi Jay,
Please do not take things I say out of context!
I'm not sure which thread you think I told you that, but its obvious to me
you are mis-quoting me!

Who wrote this message that was added to a message for an OP what I had
specially kept simple for the OP. However Jay B Harlow had again to show
that I was wrong and putted it in a way if I did not know that there where
better ways.

http://www.google.com/[email protected]

I hope this helps?

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
The article that I referenced in that messages tells you when you would use
a StringBuilder and multiple appends and tells you when you would use String
concatenation.

You need to read the article itself, to know when to use which.

Hope this helps
Jay
 
C

Cor Ligthert

Jay,
The article that I referenced in that messages tells you when you would use
a StringBuilder and multiple appends and tells you when you would use String
concatenation.

You need to read the article itself, to know when to use which.
Again, you give an answer if you are the only one who knows this or someone
who wrote a book. You always pass the fact that there can be in books
errors, which are edited in the next edition and therefore not forever true.
When somebody else give you an answer in the same way as you do to show you
this behaviour, you only become angry, so I do not do that anymore, it is
without any sense.

Did you read this conclusion about the article especially about the
stringbuilder?

Conclusion
The conclusion to be drawn from these test results is really very
straightforward. You should be using the StringBuilder class for all but the
most trivial string concatenation (or replace) operations. The extra effort
required to use the StringBuilder class is negligible and is far outweighed
by the potential performance and scalability benefits to be gained.

As I have tested and showed on this newsgroup, is that not forever for the
Stringbuilder replace true, however I have tested it as well for the
situation as stated in this thread. There are maybe situations where it
cannot be that way, however I think the conclusion from this article should
mean that using the stringbuilder instead of concatenation is preferable.

I hope this helps

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
I hope you will understand why I cannot respond to this static.

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top