littleguru wrote:After trying it in a little console application this are the results with 10,000,000 iterations.
string.Concat: 00:00:01.7713710
StringBuilder: 00:00:03.2878755
DateTime start = DateTime.Now;
for (int i = 0; i < 10000; i++)
{
string s1 = "Hello";
string s2 = ", ";
string s3 = "World";
string s4 = "!";
string s = string.Concat(s1, s2, s3, s4);
}
DateTime end = DateTime.Now;
Console.WriteLine(end - start);
start = DateTime.Now;
for (int i = 0; i < 10000; i++)
{
string s = null;
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(", ");
sb.Append("World");
sb.Append("!");
s = sb.ToString();
}
end = DateTime.Now;
Console.WriteLine(end - start);
Console.ReadLine();
To make the test fair we should remove the bold line, declare s on the same line as its initialization, and then rerun it (or you could, instead, set s to null in the first loop also.)
string s = sb.ToString();
EDIT: StringBuilder beat out the Concat on my Vista RTM machine, but it's reversed on my XP box.