9

what is the difference between these two methods?

Is one more efficient than the other?

I was thinking maybe the AppendText() uses a method similar to the StringBuilder, ie it uses its own cache instead of creating and appending a new string each time, is that true?

Thanks.

H H
  • 263,252
  • 30
  • 330
  • 514
David Klempfner
  • 8,700
  • 20
  • 73
  • 153
  • 1
    Look at it in a reflector, AppendText simply appends via calculating the end position then adding via .SelectedText – Alex K. Dec 17 '13 at 11:05
  • Documentation: http://msdn.microsoft.com/library/system.windows.forms.textboxbase.appendtext(v=vs.110).aspx – Felipe Oriani Dec 17 '13 at 11:10

3 Answers3

18

As it is clearly mentioned in Remarks section of MSDN Documentation

The AppendText method enables the user to append text to the contents of a text control without using text concatenation, which, can yield better performance when many concatenations are required.

Your question,

what is the difference between these two methods?

We all know how TextBox.Text += something; will work i.e. creating and appending a new string each time but how AppendText works I could not find any code snippet whether internally it uses StringBuilder or something else.

Is one more efficient than the other?

I think answer to above question will depend on the situation, (Based on Test case observation)

if Multiline property is set to false then Concatenation (+=) yields better results but on other hand Multiline property is set to true then AppendText yields far better performance.

EDIT After reading the comment from Rawling I made a custom win-form solution in which I had a simple textbox in which I appended a simple string hello 10000 times using a simple for-loop

    private void btnAppendText_Click(object sender, EventArgs e)
    {
        txtText.Text = string.Empty;
        DateTime startTime = DateTime.Now;
        for (int i = 0; i < 10000; i++)
        {
            txtText.AppendText(s);
        }
        DateTime endTime = DateTime.Now;
        txtTime.Text = (endTime.Ticks - startTime.Ticks).ToString();
    }

    private void btnConcante_Click(object sender, EventArgs e)
    {
        txtText.Text = string.Empty;
        DateTime startTime = DateTime.Now;
        for (int i = 0; i < 5000; i++)
        {
            txtText.Text += s;
        }
        DateTime endTime = DateTime.Now;
        txtTime.Text = (endTime.Ticks - startTime.Ticks).ToString();
    }

Output were very surprising,
TEST 1: Multiline property is true I had to reduce the iteration to half i.e. 5000 for text concatenation as it was taking a very long time.

btnAppendText_Click output on txtTime was 37222129 almost 3-4 seconds for 10000 iteration
btnConcante_Click output on txtTime was 14449906487 more than 25 minutes for only 5000 iterations.

From the above result it is really clear that, AppendText is much faster and more efficient (when Multiline is true) than Concatenation

TEST 2: Multiline property is false

btnConcante_Click output on txtTime was 39862280 almost 3-4 seconds for 10000 iteration
btnAppendText_Click output on txtTime was 1043279672 almost 2-3 minutes for 10000 iteration

From the above result it is really clear that, Concatenation is faster and more efficient (when Multiline is false) than AppendText

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
  • Surely this can be considered similar to the benefit you get from using StringBuilder compared to +=, when many concatenations are needed? – David Klempfner Dec 17 '13 at 11:20
  • 1
    The MSDN documentation is actually rubbish. `AppendText` is not more efficient than concatenation. I ran a performance test and `.Text +=` is an orders of magnitude quicker than `.AppendText`. – Rawling Dec 17 '13 at 13:06
  • @dbw - not true - we have reflector :) Microsoft code isn't protected from being looked upon (on purpose) – Ondrej Svejdar Dec 17 '13 at 13:24
  • 1
    @Rawling **.Text +=** is not faster see my edited answer – Deepak Bhatia Dec 17 '13 at 14:24
  • 1
    @Rawling was there something else on your computer or in your test case, I am using Window Application on .net framework 4 client profile, can you provide some details about your test case.... – Deepak Bhatia Dec 17 '13 at 14:58
  • @user2063755 Not sure about it because I do not know how `AppendText` is implemented by Microsoft as I can not find code for it – Deepak Bhatia Dec 17 '13 at 15:08
  • 2
    @dbw I've tried to replicate your test as closely as possible (switched to .NET 4 client profile, switched to using a class variable rather than a string literal, adding 5 characters rather than one, looping 10k rather than 1k times) and my `+=` loop still finishes in 3s while my `AppendText` runs for about 2 minutes. I guess the MSDN documentation is not total rubbish - it's correct to say it *can* yield better performance since you've witnessed it! - but I'd *love* to know why we're seeing such opposite results. As with all performance questions, it's up to OP to profile his real-world use! – Rawling Dec 17 '13 at 15:15
  • can you guys let us know the outcome? I'm also interested. – David Klempfner Dec 18 '13 at 05:51
0

The AppendText has nothing to do with StringBuilder. The Text method actually seems simpler (an possibly more performant). See source code of those two methods for reference:

public void AppendText(string text)
{
    if (text.Length > 0)
    {
        int start;
        int length;
        this.GetSelectionStartAndLength(out start, out length);
        try
        {
            int endPosition = this.GetEndPosition();
            this.SelectInternal(endPosition, endPosition, endPosition);
            this.SelectedText = text;
        }
        finally
        {
            if (base.Width == 0 || base.Height == 0)
            {
                this.Select(start, length);
            }
        }
    }
}


public override string Text {
  get {
    return base.Text;
  }
  set {
    if (value != base.Text) {
      base.Text = value;
      if (base.IsHandleCreated) {
        base.SendMessage(185, 0, 0);
      }
    }
  }
}
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
  • I might be wrong, but it doesn't even appear to persist a selection properly :-/ E.g. I've put a `KeyDown` handler that calls `AppendText` when you hit Control, and it still clears any selection you've maide. – Rawling Dec 17 '13 at 13:12
  • @Rawling - thanks for the update, I actually guessed what it does from source code (head compiler) rather than actually trying to see how it works in real life :) I've edited my answer accordingly. – Ondrej Svejdar Dec 17 '13 at 13:28
0

As a complement to dbw (and in case someone can find where I've made a mistake), here's my performance test:

private void Form1_Click(object sender, EventArgs e)
{
    Stopwatch sw = new Stopwatch();

    sw.Reset();
    textBox1.Text = "";
    sw.Start();
    for (int i = 0; i < 10000; i++)
    {
        textBox1.Text += s;
    }
    sw.Stop();
    var e1 = sw.Elapsed;

    sw.Reset();
    textBox1.Text = "";
    sw.Start();
    for (int i = 0; i < 10000; i++)
    {
        textBox1.AppendText(s);
    }
    sw.Stop();
    var e2 = sw.Elapsed;
}

I see e1 with about 3 seconds and e2 with about 2 minutes.

Rawling
  • 49,248
  • 7
  • 89
  • 127