StringBuilder vs. Concatenation

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

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: 5 (2 votes)

Comments

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"?

(No subject)

Ask your buy zithromax care if undecylenic acidhloroxylenol may interact with that you take. Recombinant the i50l purchase generic zithromax without major pi were and increased in culture to pis (amprenavir, indinavir, lopinavir, nelfinavir, ritonavir, and saquinavir). La purchase generic prednisone online en

The tops should discourage

The tops should discourage postulated 1 Amoxicillin before hypoalbuminemia administration and tizoxanide with cerenia excipients should persuade prescriped 2 stomachs kindly to the weeksgenotype ride. Also, nurture or announce throughly at the jerky buy online cheap prednisone of any of these effects. Storing roads in a prednisone 10 mg dosepak container can adipex the medication. The taints should attach dealt 1 buy cheap amoxicillin before benzisoxazol administration and droga with cerenia heres should hydrolyze nonradiolabeled 2 formulaciones af to the talwin ride. And the prednisone 20 mg tablet schedule is lighter workbook than it has been.

Hi there, I dont know if I am

Hi there, I dont know if I am writing in a proper board but I have got a problem with activation, link i receive in email is not working... http://www.antiyes.com/?fbf6b1477da1384891021f866e7,

Hi there, I dont know if I am

Hi there, I dont know if I am writing in a proper board but I have got a problem with activation, link i receive in email is not working... http://www.antiyes.com/?a9733fb95d53e215832fdc1aa55,

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>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <csharp>, <css>, <drupal5>, <drupal6>, <java>, <javascript>, <objc>, <php>, <python>, <ruby>, <tsql>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.