
StringBuilder appends different data types with overloads. What data types are fastest to Append? We can optimize StringBuilder code by choosing between data types when possible. We investigate the performance of different data types on StringBuilder.
StringBuilder parameter types test Append int: 12677 ms Append char: 1328 ms [faster] Append bool: 2268 ms Append string 1: 2095 ms [faster] Append char: 1344 ms [faster] Append string 2: 1904 ms

First, when you call the Append or AppendLine methods on StringBuilder, you can pass in many different types. There are 19 overloads, many of which specify different types. Here we test the types for performance. This program is a simple benchmark framework that compares how similar data is appended.
This C# StringBuilder benchmark measures Append performance. It tests data types.
Program that tests data types [C#]
using System;
using System.Diagnostics;
using System.Text;
class Program
{
static void Main()
{
const int m = 100000;
// Char versus Int
Stopwatch s1 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
StringBuilder s = new StringBuilder(1000);
for (int v = 0; v < 1000; v++)
{
s.Append(1);
}
}
s1.Stop();
Stopwatch s2 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
StringBuilder s = new StringBuilder(1000);
for (int v = 0; v < 1000; v++)
{
s.Append('1');
}
}
s2.Stop();
// Bool versus string
Stopwatch s3 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
StringBuilder s = new StringBuilder(4000);
for (int v = 0; v < 1000; v++)
{
s.Append(true);
}
}
s3.Stop();
Stopwatch s4 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
StringBuilder s = new StringBuilder(4000);
for (int v = 0; v < 1000; v++)
{
s.Append("True");
}
}
s4.Stop();
// Char versus string
Stopwatch s5 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
StringBuilder s = new StringBuilder(1000);
for (int v = 0; v < 1000; v++)
{
s.Append('a');
}
}
s5.Stop();
Stopwatch s6 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
StringBuilder s = new StringBuilder(1000);
for (int v = 0; v < 1000; v++)
{
s.Append("a");
}
}
s6.Stop();
Console.WriteLine("{0},{1},{2},{3},{4},{5}",
s1.ElapsedMilliseconds, s2.ElapsedMilliseconds,
s3.ElapsedMilliseconds, s4.ElapsedMilliseconds,
s5.ElapsedMilliseconds, s6.ElapsedMilliseconds);
}
}
Description. The benchmark is divided into three pairs. The first pair tests a one-character int against the same text in char format. The second pair tests a bool against the same text in string format. The third pair tests a char against its same text in string format.
Note: You can only fairly compare the three pairs against their other halves. The results were that appending a char is faster than appending an int. Appending a string was faster than appending a bool. And finally appending a char was faster than appending a string.

We compared equivalent StringBuilder appended values using the C# programming language. Note that the code samples may not be exactly equivalent in all cases. However, it is usually fastest to append chars, then strings, and then other types. Ints are the slowest here.
StringBuilder Append Performance StringBuilder Secrets