您的位置:首页 > 移动开发

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

2016-03-04 16:15 447 查看
转自:http://stackoverflow.com/questions/20632372/textbox-text-string-vs-textbox-appendtextstring

10down
voteaccepted
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 talking 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
then 25 minutes for only 5000 iteration

From the above result it is really clear that, 
AppendText
 is
much faster and efficient (when 
Multiline
is 
True
)
then 
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 efficient (when 
Multiline
 is 
false
)
then 
AppendText


10down
voteaccepted
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 talking 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
then 25 minutes for only 5000 iteration

From the above result it is really clear that, 
AppendText
 is
much faster and efficient (when 
Multiline
is 
True
)
then 
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 efficient (when 
Multiline
 is 
false
)
then 
AppendText


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: