473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C# : When exactly should a StringBuilder be used?

Hi!

I'm very interesting in when to use exactly the StringBuilder?

For example for something like this?:

String strTest1 = "This";
String strTest2 = "Test";
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append("is a "). Append(stbTest);
can someone provide some sample codes when to use a StringBuilder?

And can someone tell me his experience about the performance of a
StringBuilder?
Regards,
gicio
Nov 15 '05 #1
8 1268
A StringBuilder's main functionality is , as it's name implies, to build
strings. The fact is, in .Net, if you need to build a long string (using a
long loop for example) it would be much slower to accomplish by using
simple concatenation than it would be using a StringBuilder.
The speed improvement is very big. This is because internally, the
StringBuilder uses string pointers for concatenation, while a regular
concat operation actually creates a new string from each two concatenated
string, so if you do "a" + "b" you actually get 3 seperate string back,
a,c, and the result. in a loop doing this this is very slow. A
StringBuilder does not create new string instances and so is much faster.
When to use: Whenever you have a non trivial string that you need to build
dynamically using a loop (when creating XML strings for example).

Roy Osherove
http://www.iserializable.com

On Sun, 9 Nov 2003 19:06:31 +0100, <gi***@web.de> wrote:
Hi!

I'm very interesting in when to use exactly the StringBuilder?

For example for something like this?:

String strTest1 = "This";
String strTest2 = "Test";
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append("is a "). Append(stbTest);
can someone provide some sample codes when to use a StringBuilder?

And can someone tell me his experience about the performance of a
StringBuilder?
Regards,
gicio

Nov 15 '05 #2
Hello!
When to use: Whenever you have a non trivial string that you need to build
dynamically using a loop (when creating XML strings for example).


When building Xml; Try to use the specific writers (e.g. the XmlTextWriter).
They support indented writing and helps you programatically write more
readably (and maintainable) code.

Otherwise the StringBuilder is a powerful class.

--
venlig hilsen / with regards
anders borum
--
Nov 15 '05 #3
As the number of modification operations heads towards 10 and above,
StringBuilder becomes faster. If the number of modifications is small (as
it is in your example), String will be faster.

Nick Wienholt, MVP
Maximizing .NET Performance
http://www.apress.com/book/bookDisplay.html?bID=217
Sydney Deep .NET User Group www.sdnug.org

<gi***@web.de> wrote in message news:bo*************@news.hansenet.net...
Hi!

I'm very interesting in when to use exactly the StringBuilder?

For example for something like this?:

String strTest1 = "This";
String strTest2 = "Test";
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append("is a "). Append(stbTest);
can someone provide some sample codes when to use a StringBuilder?

And can someone tell me his experience about the performance of a
StringBuilder?
Regards,
gicio

Nov 15 '05 #4
Doesn't that depend on the size of the strings being concatenated? The
longer the strings, the more memory to be copied ..

--
venlig hilsen / with regards
anders borum
--
Nov 15 '05 #5
Jj
StringBuilder is more efficient when you do string concatenating, especially
when you do that a lot vs. using += operator.

Jianjun

<gi***@web.de> wrote in message news:bo*************@news.hansenet.net...
Hi!

I'm very interesting in when to use exactly the StringBuilder?

For example for something like this?:

String strTest1 = "This";
String strTest2 = "Test";
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append("is a "). Append(stbTest);
can someone provide some sample codes when to use a StringBuilder?

And can someone tell me his experience about the performance of a
StringBuilder?
Regards,
gicio

Nov 15 '05 #6
> Doesn't that depend on the size of the strings being concatenated? The
longer the strings, the more memory to be copied ..


Yep - that's why there is no precise number of operations where it can be
said StringBuilder is better. Somewhere between 3 and 8 is what I have seen
in benchmarks, so my general recommendation is once you're at 10,
StringBuilder is definitely the way to go. The performance difference
between the two options is not dramatic till you start doing lots of
operations, so there is no point worrying about whether 4 or 7 is the
optimum number for a particular case.

Nick
Nov 15 '05 #7
Aside from the other answers you've received, sometimes depending on what
you're doing it's actually better to use a memory stream. In some cases
you'll get 50-200% speed improvements. But we're talking *large* strings =)
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
<gi***@web.de> wrote in message news:bo*************@news.hansenet.net...
Hi!

I'm very interesting in when to use exactly the StringBuilder?

For example for something like this?:

String strTest1 = "This";
String strTest2 = "Test";
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append("is a "). Append(stbTest);
can someone provide some sample codes when to use a StringBuilder?

And can someone tell me his experience about the performance of a
StringBuilder?
Regards,
gicio

Nov 15 '05 #8
What about doing lots of small operations? One could incur the overhead of
creating a stringbuilder, then clear it after each small operation (say
contat of 5 strings.) Ala:

sb.append(str1)
sb.append(str2)
(etc)
response.write(sb.tostring)
sb.remove(0, sb.length)

sb.append(newstr1)
sb.append(newstr2)

etc.
Thoughts?

-- russ
"Nick Wienholt" <go*********@hotmail.com> wrote in message
news:e6**************@TK2MSFTNGP09.phx.gbl...
As the number of modification operations heads towards 10 and above,
StringBuilder becomes faster. If the number of modifications is small (as
it is in your example), String will be faster.

Nick Wienholt, MVP
Maximizing .NET Performance
http://www.apress.com/book/bookDisplay.html?bID=217
Sydney Deep .NET User Group www.sdnug.org

<gi***@web.de> wrote in message news:bo*************@news.hansenet.net...
Hi!

I'm very interesting in when to use exactly the StringBuilder?

For example for something like this?:

String strTest1 = "This";
String strTest2 = "Test";
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append("is a "). Append(stbTest);
can someone provide some sample codes when to use a StringBuilder?

And can someone tell me his experience about the performance of a
StringBuilder?
Regards,
gicio


Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: | last post by:
Hi! I'm very interesting in when to use exactly the StringBuilder? For example for something like this?: String strTest1 = "This"; String strTest2 = "Test"; StringBuilder stbTest = new...
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
20
by: Alvin Bruney | last post by:
On the advice of a user, I've timed stringbuilder v string. Here are the results. Here are the numbers: Total # queries 3747 Time in Milliseconds StringBuilder: String...
9
by: Bjorn Abelli | last post by:
Hi all, When I run a normal application I can get who started a process with the following example: Process myProcesses = Process.GetProcesses(); foreach(Process p in myProcesses) {...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
8
by: Henning M | last post by:
Hi, I'm trying to use stringbuilder to collect a list of strings. (as suggested by Claes Bergefall) Declare Auto Function CM_Get_Device_ID_List Lib "cfgmgr32.dll" (ByVal pszFilter As String,...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.