StringBuilder vs. Concatenation

Share this

1: Intro

After the if vs. ternary deathmatch I started wondering just how much slower string concatenation was versus using a StringBuilder. Below are the test and results, not very surprising though.

2: Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
 
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] words = ("Quisque in neque at orci congue " +
                            "tempor. Donec id eros velit, eu " +
                            "sodales est. Sed purus eros, tempor  " +
                            "id ultricies eget, volutpat a mauris.  " +
                            "Suspendisse potenti. Suspendisse id  " +
                            "ligula nec felis mattis pellentesque  " +
                            "nec quis ipsum. Vivamus sed metus nunc,  " +
                            "non eleifend ligula. Sed mollis sagittis  " +
                            "pellentesque. Nam sit amet ante ut risus  " +
                            "pulvinar rhoncus. Vestibulum molestie  " +
                            "feugiat leo sed placerat. Sed consectetur " +
                            "velit ut magna molestie molestie tincidunt " +
                            "magna viverra. Quisque eu diam lacus. Sed  " +
                            "blandit, felis vel sollicitudin pellentesque, " +
                            "sapien nisl ullamcorper mauris, dapibus  " +
                            "accumsan diam augue et diam. Ut suscipit  " +
                            "nibh pretium nisi pulvinar vitae condimentum  " +
                            "felis mattis. Donec justo orci, gravida in  " +
                            "aliquam et, cursus sit amet sapien. Pellentesque " +
                            "mauris ipsum, ornare ac blandit nec, pretium  " +
                            "nec purus. Curabitur malesuada, lectus at  " +
                            "fermentum tempus, risus orci condimentum  " +
                            "lorem, id consequat dolor velit sed arcu.  " +
                            "Duis hendrerit rutrum tellus, a dignissim  " +
                            "velit facilisis ac. Curabitur pretium quam a " +
                            "metus sagittis mattis. Cum sociis natoque  " +
                            "penatibus et magnis dis parturient montes,  " +
                            "nascetur ridiculus mus.").Split(" ".ToCharArray());
 
 
            TimeSpan concatenationTestTime = TestConcatenation(words);
            TimeSpan stringbuilderTestTime = TestStringBuilder(words);
 
            Console.WriteLine(string.Format("StringBuilder: {0}", stringbuilderTestTime));
            Console.WriteLine(string.Format("Concatenation: {0}", concatenationTestTime));
 
            Console.ReadLine();
 
        }
 
        static TimeSpan TestConcatenation(string[] words)
        {
            string finalResult = string.Empty;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 100; i++)
            {
                foreach (string word in words)
                {
                    finalResult = finalResult + word;
                    finalResult = finalResult + " ";
                }
            }
            sw.Stop();
            return sw.Elapsed;
        }
 
        static TimeSpan TestStringBuilder(string[] words)
        {
            string finalResult = string.Empty;
            StringBuilder full = new StringBuilder();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 100; i++)
            {
                foreach (string word in words)
                {
                    full.Append(word);
                    full.Append(" ");
                }
            }
            finalResult = full.ToString();
            sw.Stop();
            return sw.Elapsed;
        }
    }
}

3: Results

The not too surprising results: StringBuilder is a lot faster than string concatenation.
Results



AttachmentSize
StringBuilder vs. Concatenation VS2008 Project32.69 KB
Your rating: None Average: 3 (4 votes)

I didn't see those results

I didn't see those results coming...

Can I guess as to the translation of the words? How about "These go to eleven"?

Post new comment

The content of this field is kept private and will not be shown publicly.
 
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <drupal5>, <drupal6>, <html4strict>, <java>, <javascript>, <objc>, <php>, <python>, <ruby>, <tsql>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.