I want to talk with you about the performance difference between string concatenation and doing the same with a stringbuilder. I can tell you already that the bottomline is that using a stringbuilder to concatenate string is much faster.
Why? That is because strings are immutable which means that once strings are created they cannot be changed. But I can change a string you say. Oh right, that is right. What I mean is that a string buffer is not reused when a new value is assigned to a string variable. A new buffer is created to hold the new value. The old one is left behind to be recycled by the garbage collector. Not so very efficient memory use or is it? The creation of new buffers for new values for strings impacts the performance of an application if alot of strings are changed because the garbage collector has to free memory many more times than normal.
A stringbuilder does not have this problem because it works internally with flexible memory buffer that is extended dynamically. This is a fast and memory efficient model.
Look at this picture. It tells more than a 1.000 words.
So it is clear that when you want to concatenate alot of strings it is better to use a stringbuilder.
The test code I used is this:
10 static void Main(string[] args)
11 {
12 Console.WriteLine("Start String vs. StringBuilder performance test.");
13 Console.WriteLine();
14 Console.WriteLine("Started string performance test.");
15 Console.WriteLine();
16
17 // Create a stopwatch to time the tests
18 Stopwatch stopwatch = new Stopwatch();
19
20 // Test the performance of strings allocation
21 stopwatch.Start();
22 string testString = string.Empty;
23 for (int i = 0; i < 100000; i++)
24 {
25 testString += i.ToString();
26 }
27 stopwatch.Stop();
28
29 Console.WriteLine("Ended string performance test.");
30 Console.WriteLine();
31
32 // Write the elapsed time
33 Console.WriteLine("100,000 string allocations time elapsed : {0} ms",
stopwatch.ElapsedMilliseconds);
34
35 stopwatch.Reset();
36
37 Console.WriteLine("Started StringBuilder performance test.");
38 Console.WriteLine();
39
40 // Test the performance of stringbuilder allocation
41 stopwatch.Start();
42 StringBuilder sb = new StringBuilder();
43 for (int i = 0; i < 100000; i++)
44 {
45 sb.Append(i);
46 }
47 stopwatch.Stop();
48
49 Console.WriteLine("Ended StringBuilder performance test.");
50 Console.WriteLine();
51
52 // Write the elapsed time
53 Console.WriteLine("100,000 stringbuilder allocations time elapsed : {0} ms",
stopwatch.ElapsedMilliseconds);
54
55 stopwatch.Reset();
56
57 // Keep window open
58 Console.ReadKey();
59 }