TextBox.Text + ="串英寸; VS TextBox.AppendText(QUOT;串QUOT;); [英] TextBox.Text += "string"; vs TextBox.AppendText("string");
问题描述
就是这两种方法之间的区别?
what is the difference between these two methods?
比其他有效率的吗?
我想也许在AppendText通过()使用类似的StringBuilder的方法,也就是说,它使用它自己的缓存,而不是创建和附加一个新的字符串每次,是真的吗?
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通过方法使用户将文本追加到文本控件的内容,无需使用文本串联,其中,能产生时需要许多串联更好的表现。
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.
您的问题,
就是这两种方法之间的区别?
what is the difference between these two methods?
我们都知道 TextBox.Text + =东西;
将工作即创建和每次追加一个新的字符串,但如何 AppendText通过
作品无论在内部,它使用我找不到任何代码段的StringBuilder
还是其他什么东西。
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.
比其他有效率的吗?
我想回答上面的问题将视情况而定,(根据测试的情况下观察)
I think answer to above question will depend on the situation, (Based on Test case observation)
如果 多
属性设置为 假
和级联(+ =)会产生更好的结果,但是,另一方面 多
属性设置为 真
和 AppendText通过
性能收益率远越好。
if
Multiline
property is set tofalse
then Concatenation (+=) yields better results but on other handMultiline
property is set toTrue
thenAppendText
yields far better performance.
修改阅读的注释从Rawling 我做了一个自定义双赢的形式解决方案,其中我有一个简单的文本
中我追加一个简单的字符串你好
10000 使用一个简单的时间 for循环
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();
}
输出是非常令人惊讶的,结果
测试1:多行属性为true
我不得不迭代减少一半,即5000 text并置,因为它是说很长的时间。
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 talking very long time
btnAppendText_Click
产量的 37222129
几乎3-4秒为10000迭代结果
btnConcante_Click
输出上 txtTime
为 14449906487
然后25分钟,仅5000迭代
btnAppendText_Click
output ontxtTime
was37222129
almost 3-4 seconds for 10000 iteration
btnConcante_Click
output ontxtTime
was14449906487
more then 25 minutes for only 5000 iteration
从上面的结果真的很清楚的是, AppendText通过
更快,效率(当多行
是真
),那么级联
From the above result it is really clear that, AppendText
is much faster and efficient (when Multiline
is True
) then Concatenation
TEST 2:多行属性设置为false
btnConcante_Click
在 txtTime
为 39862280
几乎3-4秒为10000迭代结果
btnAppendText_Click
在 txtTime
为输出 1043279672
差不多2-3分钟10000次迭代
btnConcante_Click
output ontxtTime
was39862280
almost 3-4 seconds for 10000 iteration
btnAppendText_Click
output ontxtTime
was1043279672
almost 2-3 minutes for 10000 iteration
从上面的结果真的很明显,级联速度更快,效率(当多
是假
),那么 AppendText通过
From the above result it is really clear that, Concatenation is faster and efficient (when Multiline
is false
) then AppendText
这篇关于TextBox.Text + ="串英寸; VS TextBox.AppendText(QUOT;串QUOT;);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!