Reputation: 15807
Update
Please see this question: Does StringBuilder use more memory than String concatenation?. I believe Dan Taos' answers my question. I wanted to make sure that his answer is still sound as it is dated 2010? I believe this also answers my question: https://coders-corner.net/2014/08/20/concatenate-strings-in-c-operator-vs-string-concat-vs-stringbuilder/
Original Question
This approach is frowned upon using C#:
public string getMessage()
{
string message = "Hello";
string message += " my";
string message += " name";
string message += " is";
string message += " bert";
return message;
}
This is better:
public string getMessage()
{
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(" my");
sb.Append(" name");
sb.Append(" is");
sb.Append(" Bert");
return message.ToString();
}
How does this approach compare to the second approach:
public string getMessage()
{
return string.Concat("Hello", " my", " name", " is", " Bert");
}
I like the third approach because it is only one line of code. Are there any implications of using the third approach? I have looked at the .NET reference here: https://referencesource.microsoft.com/#mscorlib/system/string.cs,8281103e6f23cb5c and specifically the method with signature: public static String Concat(params String[] values). However, I still wanted to confirm?
Upvotes: 1
Views: 857
Reputation: 2523
This depends on what you want. If you are contatenating 2 string than I suppose this will be faster than StringBuilder as there is a certain overhead in instantiating StringBuilder class. If, on the other hand, you are contatenating 100 strings than StringBuilder class will be much faster. The reason why it is faster is that it uses a buffer rather than normal string contatenation(string is immutable).
string.Concat() does not use StringBuilder internally so this assumption is wrong.
Try to look at the string.Concat overload and you might get a better idea when to use string.Concat and when to use StringBuilder. You will notice that there are couple of overloads(10 to be exact but lets look at these 5).
string.Concat(arg0)
string.Concat(arg0, arg1)
string.Concat(arg0, arg1, arg2)
string.Concat(arg0, arg1, arg2, arg3)
string.Concat(params string[])
If you make a benchmark where you use all these methods along with StringBuilder you will notice that somewhere around 4 string contatenations StringBuilder will take the lead. This is because first 4 string.Concat() methods are more optimized than the last one that simply takes how many arguments as is needed.
Interesting enough, if you would add another measuring series but this time for StringBuilder where you instantiate it with capacity than that StringBuilder would take the lead since there is an overhead when StringBuilder reaches it's current capacity ceiling.
EDIT: I have found my benchmark results(this was for a dissertation in 2011). This was done 100,000 times for reliability purposes. Axis X is number of string used in contatenation and axis Y is the duration.
Upvotes: 1
Reputation: 819
StringBuilder is so much optimized. It's my test:
static void ConcatenateWithOperator()
{
string txt = string.Empty;
for (int i = 0; i < 1000; i++)
{
txt += new string('x', i);
}
}
static void ConcatenateWithStringBuilder()
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 1000; i++)
{
builder.Append(new string('x', i));
}
string result = builder.ToString();
}
static void ConcatenateWithConcat()
{
string txt = string.Empty;
for (int i = 0; i < 1000; i++)
{
txt = string.Concat(txt, new string('x', i));
}
}
static void ConcatenateWithOperator2()
{
for (int i = 0; i < 1000; i++)
{
string result = new string('x', i) + new string('x', i);
}
}
static void ConcatenateWithStringBuilder2()
{
for (int i = 0; i < 1000; i++)
{
StringBuilder builder = new StringBuilder();
builder.Append(new string('x', i));
builder.Append(new string('x', i));
string result = builder.ToString();
}
}
DateTime start1 = DateTime.Now;
ConcatenateWithOperator();
Console.WriteLine((DateTime.Now - start1).TotalSeconds);
DateTime start2 = DateTime.Now;
ConcatenateWithStringBuilder();
Console.WriteLine((DateTime.Now - start2).TotalSeconds);
DateTime start3 = DateTime.Now;
ConcatenateWithConcat();
Console.WriteLine((DateTime.Now - start3).TotalSeconds);
DateTime start4 = DateTime.Now;
ConcatenateWithOperator2();
Console.WriteLine((DateTime.Now - start4).TotalSeconds);
DateTime start5 = DateTime.Now;
ConcatenateWithStringBuilder2();
Console.WriteLine((DateTime.Now - start5).TotalSeconds);
And the results:
0,3087516 // many strings concatenated with + operator
0,0030022 // many strings concatenated with StringBuilder
0,1941643 // many strings concatenated with Concat
0,0040039 // two strings concatenated with + operator
0,004003 // two strings concatenated with StringBuilder
So, the + operator is efficient as StringBuilder only if you want to concatenate small amount of strings.
Upvotes: 0