Reinventing The Wheel

TextBox.AppendText("string"); Vs TextBox.Text += "string";

Have you ever wondered what is more efficient, using TextBox.AppendText("string"), or TextBox.Text += "string";?

I tested this out using a Windows Form application, and the results are quite interesting

In the Windows Form application, I had 4 buttons:

  • Concatenation Button
  • StringBuilder Concatenation Button
  • AppendText Button
  • StringBuilder AppendText Button

The concatenation button used textBox1.Text += "s" in a for loop of 1000 iterations.

The AppendText button used textBox1.AppendText("s") in a for loop of 1000 iterations.

The StringBuilder button equivalents did essentially the same, however instead of appending using += or the AppendText() method, it used the Append() method of the StringBuilder instance in the for loop, and then appended, or concatenated the string value of the StringBuilder instance at the end of the for loop.

Have a look at the code for each of the 4 buttons below:

private int numberOfIterations = 1000;
private void ConcatenationButton_Click(object sender, EventArgs e)
{
    textBox1.Text = string.Empty;
    DateTime startTime = DateTime.Now;
    for (int i = 0; i < numberOfIterations; i++)
    {
        textBox1.Text += "s";
    }
    DateTime endTime = DateTime.Now;
    ConcatenationLabel.Text = (endTime.Ticks - startTime.Ticks).ToString();
}

private void StringBuilderConcatenation_Click(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    textBox1.Text = string.Empty;
    DateTime startTime = DateTime.Now;
    for (int i = 0; i < numberOfIterations; i++)
    {
        sb.Append("s");
    }
    textBox1.Text += sb.ToString();
    DateTime endTime = DateTime.Now;
    ConcatenationLabel.Text = (endTime.Ticks - startTime.Ticks).ToString();
}
        

private void appendTextButton_Click(object sender, EventArgs e)
{
    textBox1.Text = string.Empty;
    DateTime startTime = DateTime.Now;
    for (int i = 0; i < numberOfIterations; i++)
    {
        textBox1.AppendText("s");
    }
    DateTime endTime = DateTime.Now;
    AppendLabel.Text = (endTime.Ticks - startTime.Ticks).ToString();
}

private void StringBuilderAppendText_Click(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    textBox1.Text = string.Empty;
    DateTime startTime = DateTime.Now;
    for (int i = 0; i < numberOfIterations; i++)
    {
        sb.Append("s");
    }
    textBox1.AppendText(sb.ToString());
    DateTime endTime = DateTime.Now;
    AppendLabel.Text = (endTime.Ticks - startTime.Ticks).ToString();
}

Results

The results of these four methods are quite interesting.

TextBox.Multiline = True

With StringBuilder
Button Time (ticks)
Concatenation 180158
AppendText 180080

AppendText() is faster by 78 ticks

Without StringBuilder
Button Time (ticks)
Concatenation 41215232
AppendText 7330947

AppendText() is faster by 33884285 ticks

TextBox.Multiline = False

With StringBuilder
Button Time (ticks)
Concatenation 10266
AppendText 20256

Concatenation is faster by 9990 ticks

Without StringBuilder
Button Time (ticks)
Concatenation 1199828
AppendText 7890616

Concatenation is faster by 6690788 ticks

As the results show, if the TextBox.Multiline property is set to true, then TextBox.AppendText() is faster.

Whereas if Multiline = false, concatenation is faster.

The use of the StringBuilder instance just shows how much more efficient it is than concatenation when building a string in a for loop.