StringBuilder

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

when I concatenate strings is it faster to use the StringBuilder or the
string.Format function?

thanks christian
 
Hi,

when I concatenate strings is it faster to use the StringBuilder or the
string.Format function?

thanks christian

You could write a program to find out then post your results here.
 
Read this:

http://blogs.msdn.com/ricom/archive/2003/12/15/43628.aspx

String.Format takes the various arguments, builds them into an object array,
creates an empty StringBuilder and then passes them along to the
StringBuilder.FormatAppend services.

That will be quite a bit more costly than just the straight append in the
easy cases. But it does offer advantages such as localizability and so
forth. It's certainly not a plain cat. You can see this happening if you
trace it all with CLRProfiler which I highly recommend.

Friday, December 19, 2003 12:49 PM by Rico Mariani
 
Hi Lau,

thanks a lot for your response.

Greetings from Munich
Christian

Lau Lei Cheong said:
Read this:

http://blogs.msdn.com/ricom/archive/2003/12/15/43628.aspx

String.Format takes the various arguments, builds them into an object array,
creates an empty StringBuilder and then passes them along to the
StringBuilder.FormatAppend services.

That will be quite a bit more costly than just the straight append in the
easy cases. But it does offer advantages such as localizability and so
forth. It's certainly not a plain cat. You can see this happening if you
trace it all with CLRProfiler which I highly recommend.

Friday, December 19, 2003 12:49 PM by Rico Mariani

Christian Havel said:
Hi,

when I concatenate strings is it faster to use the StringBuilder or the
string.Format function?

thanks christian
 
StringBuilder is a better option if you simply want to concatenate strings.
String.Format() also uses StringBuilder object internally but provides
aditional formatting options.

String.Format() can be more costly that StringBuilder if the only
requirement is to append strings.
 
Christian said:
Hi,

when I concatenate strings is it faster to use the StringBuilder or the
string.Format function?

thanks christian

For a known and limited number of strings, string.Concat is more efficient:

string output = string.Concat("Length: ", len.ToString(), "m");

This will also use the string.Concat method in the background:

string output = "Length: " + len.ToString() + "m";

You can use formatting in the ToString method also.


The string.Format method is more convenient sometimes. In most cases the
difference in performance is so small that it doesn't matter, so use the
one that you find best at the time, and optimise it only if needed.
 
If it's all in one line you could also simply use "+" to append them.
It'll compile down to String.Concat which is actually fastest.

Personally I prefer String.Format because it's easier to read and it's
almost rarely a point of performance bottleneck--you have to use it
about 100,000 times before it's worth the loss in readability.

Sam
 

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

Back
Top