Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Format and Building Strings with "+"

I want to ask what peoples thoughts are about writing strings and if there is a massive difference on performance when building a string.

I have always been told in recent years to never do the following:

string dogName = "Ralph";
DateTime bornDate = DateTime.Parse("2010-01-01");

string fullText = "I am a Dog and my name is " + dogName + " and i was born on the " + bornDate.ToString("dd/MM/yyyy");

And always told to something similar as below.

string dogName = "Ralph";
DateTime bornDate = DateTime.Parse("2010-01-01");

string fullText2 = String.Format("I am a Dog and my name is {0} and i was born on the {1:dd/MM/yyyy}", dogName, bornDate);

I see the reason for using the later statement, but does anyone know what type of performance issues there are with the first one.

And with the StringBuilder Object instead of using

string str = "Adding this comment to the string\n"
str += "Then Add this line";

and using the following:

StringBuilder sb = new StringBuilder();

sb.AppendLine("Add This Comment");
sb.AppendLine("Then add this comment");

string output = sb.ToString();

I have a handful of developers in my team at work that are a bit old school VB6 programmers and i want to explain to them why it a bad idea.

They do the Initial code example constantly for In Code SQL Statements.

If it is of course :)

like image 462
Robbie Tapping Avatar asked Feb 27 '11 01:02

Robbie Tapping


2 Answers

The performance of a concatenation operation for a String or StringBuilder object depends on how often a memory allocation occurs. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation only allocates memory if the StringBuilder object buffer is too small to accommodate the new data. Consequently, the String class is preferable for a concatenation operation if a fixed number of String objects are concatenated. In that case, the individual concatenation operations might even be combined into a single operation by the compiler. A StringBuilder object is preferable for a concatenation operation if an arbitrary number of strings are concatenated; for example, if a loop concatenates a random number of strings of user input.

Source: http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

like image 164
Cosmin Avatar answered Oct 06 '22 01:10

Cosmin


StringBuilder is really only useful in a performance sense when you are building a very large string which requires a lot of concatenation. For the small samples you provided I think that using the + operator is just fine.

When it comes to String.Format, I think it should be used to improve the readability or flexibility of the code but not really for performance reasons. String.Format also has the added benefit of supporting localization which is not really easy when using the + operator.

I have never run into a situation in my experience where concatenation was causing a serious perf problem so I always choose the most readable code before trying to optimize for performance.

like image 30
Luke Foust Avatar answered Oct 06 '22 00:10

Luke Foust