Click here to Skip to main content
10,500,000 members (50,167 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 18 Jan 2007

StringConc vs StringBuilder Test

, 18 Jan 2007
Rate this:
Please Sign up or sign in to vote.
StringConc vs StringBuilder Test
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Sample Image - StringConc_vs_StringBuild.gif


Because of the messages I received at one of my last article (Meta Tag Generator) regarding the efficiency of using StringBuilder or String Concatenation I decided to write a short application to demonstrate that StringBuilder is efficient only if the number of concatenations is enough big (in this case bigger than 500).
Thanks also to:

The code is very simple:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
    public partial class Form1 : Form
        public Form1()

        public void UpdateTime()
            int iterations = Int32.Parse(txtIterations.Text);   //how many times
            string theString = txtTheString.Text;               //text to be used
            DateTime strCall = DateTime.Now;
            string targetString = null;
            for (int x = 0; x < iterations; x++)
                targetString += theString;                      //concatenation using string concatenation 
            TimeSpan time = (DateTime.Now - strCall);           //time needed for simple concatenation
            txtConcatTime.Text = time.TotalMilliseconds.ToString();

            DateTime inCall = DateTime.Now;
            StringBuilder sb = new StringBuilder(theString);
            for (int x = 0; x < iterations; x++)
                sb.Append(theString);                           //use of StringBuilder
            time = (DateTime.Now - inCall);                     //time needed for StringBuilder to finish the job
            txtStringBTime.Text = time.TotalMilliseconds.ToString();

        private void button1_Click(object sender, EventArgs e)
            UpdateTime();                                       //run the test

How the test works:
in Iterations textbox write how many times do you want to "concatenate" the string from TheString textbox (it is not important how long is this text). Pressing the button Test you'll see the result (time needed to do the job with string conc (+=) and StringBuilder)

Case1: Iterations = 10 => ConcatTime = 0; StringBTime = 0;
Case2: Iterations = 100 => ConcatTime = 0; StringBTime = 0;
Case3: Iterations = 500 => ConcatTime = 2.05; StringBTime = 0;
Case4: Iterations = 1000 => ConcatTime = 15.52; StringBTime = 0;
Case4: Iterations = 10000 => ConcatTime = 3881.55; StringBTime = 0;

My Conclusion: if the numbers of concatenations is not bigger than 100, the run time is the same (zero). This means that is not important what method you use, or use the method you like more.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Israel Israel
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralStopwatch use Pin
zeltera28-Jan-07 8:30
memberzeltera28-Jan-07 8:30 
GeneralTotalMilliseconds is only accurate to 10ms Pin
GregMiller20-Jan-07 9:37
memberGregMiller20-Jan-07 9:37 
GeneralSource code missing Pin
TBermudez18-Jan-07 5:46
memberTBermudez18-Jan-07 5:46 
GeneralInaccurate Observation Pin
Mike Doyon18-Jan-07 3:27
memberMike Doyon18-Jan-07 3:27 
GeneralRe: Inaccurate Observation Pin
Jan Seda18-Jan-07 6:11
memberJan Seda18-Jan-07 6:11 
GeneralNot correct Pin
Jan Seda18-Jan-07 2:02
memberJan Seda18-Jan-07 2:02 
GeneralRe: Not correct Pin
zeltera18-Jan-07 2:08
memberzeltera18-Jan-07 2:08 
GeneralRe: Not correct [modified] Pin
Jan Seda18-Jan-07 2:30
memberJan Seda18-Jan-07 2:30 
GeneralRe: Not correct Pin
Alois Kraus18-Jan-07 3:19
memberAlois Kraus18-Jan-07 3:19 
GeneralNot exact Pin
Itay Sagui18-Jan-07 1:23
memberItay Sagui18-Jan-07 1:23 
GeneralRe: Not exact Pin
zeltera18-Jan-07 1:32
memberzeltera18-Jan-07 1:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 18 Jan 2007
Article Copyright 2007 by zeltera
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid