简体   繁体   English

TextBox.Text +=“字符串”; vs TextBox.AppendText(“字符串”);

[英]TextBox.Text += “string”; vs TextBox.AppendText(“string”);

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?我在想 AppendText() 可能使用类似于 StringBuilder 的方法,即它使用自己的缓存而不是每次都创建和附加一个新字符串,这是真的吗?

Thanks.谢谢。

As it is clearly mentioned in Remarks section of MSDN Documentation正如MSDN 文档的备注部分中明确提到的那样

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. AppendText 方法使用户能够在不使用文本串联的情况下将文本附加到文本控件的内容,这在需要许多串联时可以产生更好的性能。

Your question,你的问题,

what is the difference between these two methods?这两种方法有什么区别?

We all know how TextBox.Text += something;我们都知道如何TextBox.Text += something; will work ie 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.将工作,即每次创建和附加一个新字符串,但AppendText是如何工作的我找不到任何代码片段,无论它在内部使用StringBuilder还是其他东西。

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.如果Multiline属性设置为false那么串联 (+=) 会产生更好的结果,但另一方面Multiline属性设置为true那么AppendText产生更好的性能。

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编辑阅读Rawling评论后,我制作了一个自定义的 win-form 解决方案,其中我有一个简单的textbox在其中我使用简单的for-loop附加了一个简单的字符串hello 10000

    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 ie 5000 for text concatenation as it was taking a very long time.测试 1:多行属性为真我不得不将迭代次数减少到一半,即 5000 次用于文本连接,因为它花费了很长时间。

btnAppendText_Click output on txtTime was 37222129 almost 3-4 seconds for 10000 iteration btnAppendText_Click输出txtTime37222129差不多3-4秒为10000迭代
btnConcante_Click output on txtTime was 14449906487 more than 25 minutes for only 5000 iterations. btnConcante_Click输出txtTime14449906487超过25分钟,仅5000迭代。

From the above result it is really clear that, AppendText is much faster and more efficient (when Multiline is true ) than Concatenation从上面的结果可以清楚地看出, AppendTextConcatenation更快、更高效(当Multilinetrue

TEST 2: Multiline property is false测试 2:多行属性为 false

btnConcante_Click output on txtTime was 39862280 almost 3-4 seconds for 10000 iteration btnConcante_Click输出txtTime39862280差不多3-4秒为10000迭代
btnAppendText_Click output on txtTime was 1043279672 almost 2-3 minutes for 10000 iteration btnAppendText_Click输出txtTime1043279672几乎2-3分钟10000次迭代

From the above result it is really clear that, Concatenation is faster and more efficient (when Multiline is false ) than AppendText从上面的结果可以清楚地看出,Concatenation 比AppendText更快、更高效(当Multilinefalse

The AppendText has nothing to do with StringBuilder. AppendText 与 StringBuilder 无关。 The Text method actually seems simpler (an possibly more performant). Text 方法实际上看起来更简单(可能更高效)。 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);
      }
    }
  }
}

As a complement to dbw (and in case someone can find where I've made a mistake), here's my performance test:作为dbw的补充(以防有人能找到我犯错的地方),这是我的性能测试:

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.我看到e1大约有 3 秒, e2大约有 2 分钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM