Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple performance test for string concatenation operations
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace StringConcat
{
class Program
{
private static int noLoops = 100000;
static void Main(string[] args)
{
Console.WriteLine("Adding one strings");
RunIterations("Simple concatenation by using + operator", foo => foo = "a");
RunIterations("string.Format", foo => foo = string.Format("{0}", "a"));
RunIterations("StringBuilder", foo =>
{
var builder = new StringBuilder(foo);
builder.Append("a");
foo = builder.ToString();
});
Console.WriteLine();
Console.WriteLine("Adding two strings");
RunIterations("Simple concatenation by using + operator", foo => foo = "a" + "lorem");
RunIterations("string.Format", foo => foo = string.Format("{0}{1}", "a", "lorem"));
RunIterations("StringBuilder", foo =>
{
var builder = new StringBuilder(foo);
builder.Append("a");
builder.Append("lorem");
foo = builder.ToString();
});
Console.WriteLine();
Console.WriteLine("Adding six strings");
RunIterations("Simple concatenation by using + operator", foo => foo = "a" + "lorem" + "ipsum" + "dolor" + "sit" + "amet");
RunIterations("string.Format", foo => foo = string.Format("{0}{1}{2}{3}{4}{5}", "a", "lorem", "ipsum", "dolor", "sit", "amet"));
RunIterations("StringBuilder", foo =>
{
var builder = new StringBuilder(foo);
builder.Append("a");
builder.Append("lorem");
builder.Append("ipsum");
builder.Append("solor");
builder.Append("sit");
builder.Append("amet");
foo = builder.ToString();
});
Console.WriteLine();
var a = "lorem";
var b = "ipsum";
var c = "dolor";
var d = "sit";
var e = "amet";
var f = "consectetur";
var g = "adipiscing";
var h = "elit";
var i = "Cras ";
var j = "semper ";
var k = "pretium ";
var l = "nulla";
var m = "quis ";
var n = "tempus ";
var o = "eros ";
var p = "rutrum";
var q = "rutrum";
var r = "rutrum";
var s = "rutrum";
var t = "rutrum";
var u = "rutrum";
var v = "rutrum";
var w = "rutrum";
var x = "rutrum";
var y = "rutrum";
var z = "rutrum";
Console.WriteLine("Adding a lot of strings");
RunIterations("Simple concatenation by using + operator", foo =>
{
foo = a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z + a + b + c;
});
// RunIterations("string.Format", foo => foo = string.Format("{0}{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}", a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z));
RunIterations("StringBuilder", foo =>
{
var builder = new StringBuilder(foo);
builder.Append(a);
builder.Append(b);
builder.Append(c);
builder.Append(d);
builder.Append(e);
builder.Append(f);
builder.Append(g);
builder.Append(h);
builder.Append(i);
builder.Append(j);
builder.Append(k);
builder.Append(l);
builder.Append(m);
builder.Append(n);
builder.Append(o);
builder.Append(p);
builder.Append(q);
builder.Append(r);
builder.Append(s);
builder.Append(t);
builder.Append(u);
builder.Append(v);
builder.Append(w);
builder.Append(x);
builder.Append(y);
builder.Append(z);
builder.Append(a);
builder.Append(b);
builder.Append(c);
foo = builder.ToString();
});
Console.WriteLine();
var num = 10;
Console.WriteLine("With a loop");
RunIterations("Simple concatenation by using + operator", foo =>
{
for (var idx = 0; idx < num; idx++)
{
foo += a;
}
});
RunIterations("StringBuilder", foo =>
{
var builder = new StringBuilder(foo);
for (var idx = 0; idx < num; idx++)
{
builder.Append(a);
}
foo = builder.ToString();
});
Console.WriteLine();
}
private static void RunIterations(string label, Action<string> concatAction)
{
Console.WriteLine(label);
Console.Write("01: ");
Test(concatAction);
Console.Write("02: ");
Test(concatAction);
Console.Write("03: ");
Test(concatAction);
Console.Write("04: ");
Test(concatAction);
Console.Write("05: ");
Test(concatAction);
Console.Write("06: ");
Test(concatAction);
Console.Write("07: ");
Test(concatAction);
Console.Write("08: ");
Test(concatAction);
Console.Write("09: ");
Test(concatAction);
Console.Write("10: ");
Test(concatAction);
}
private static void Test(Action<string> concatAction)
{
var timer = Stopwatch.StartNew();
for (var i = 0; i < noLoops; i++)
{
concatAction.Invoke(string.Empty);
}
timer.Stop();
Console.WriteLine("Elapsed: {0}", timer.Elapsed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment