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 AppendTextworks I could not find any code snippet whether internally it uses StringBuilderor 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 Multilineproperty is set to falsethen Concatenation (+=) yields better results but on other hand Multilineproperty is set to Truethen AppendTextyields far better performance. EDIT After reading the comment from Rawling I made a custom win-form solution in which I had a simple textboxin which I appended a simple string hello10000 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_Clickoutput on txtTimewas 37222129almost 3-4 seconds for 10000 iteration btnConcante_Clickoutput on txtTimewas 14449906487more then 25 minutes for only 5000 iteration From the above result it is really clear that, AppendTextis much faster and efficient (when Multilineis True) then Concatenation TEST 2: Multiline property is false btnConcante_Clickoutput on txtTimewas 39862280almost 3-4 seconds for 10000 iteration btnAppendText_Clickoutput on txtTimewas 1043279672almost 2-3 minutes for 10000 iteration From the above result it is really clear that, Concatenation is faster and efficient (when Multilineis 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 AppendTextworks I could not find any code snippet whether internally it uses StringBuilderor 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 Multilineproperty is set to falsethen Concatenation (+=) yields better results but on other hand Multilineproperty is set to Truethen AppendTextyields far better performance. EDIT After reading the comment from Rawling I made a custom win-form solution in which I had a simple textboxin which I appended a simple string hello10000 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_Clickoutput on txtTimewas 37222129almost 3-4 seconds for 10000 iteration btnConcante_Clickoutput on txtTimewas 14449906487more then 25 minutes for only 5000 iteration From the above result it is really clear that, AppendTextis much faster and efficient (when Multilineis True) then Concatenation TEST 2: Multiline property is false btnConcante_Clickoutput on txtTimewas 39862280almost 3-4 seconds for 10000 iteration btnAppendText_Clickoutput on txtTimewas 1043279672almost 2-3 minutes for 10000 iteration From the above result it is really clear that, Concatenation is faster and efficient (when Multilineis false) then AppendText |
相关文章推荐
- net.sf.json.JSONObject 和org.json.JSONObject 的差别
- Android 改变statusbar背景-沉浸式状态栏与导航栏
- 如何查看App provision profile文件中的钥匙链访问组名称
- 如何查看App provision profile文件中的钥匙链访问组名称
- 如何查看App provision profile文件中的钥匙链访问组名称
- Swift 实践之简单计算器
- ios 版本更新功能
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)
- Android图片加载框架Android-Universal-Image-Loader
- iOS获取当前地理位置文本
- Application 应用程序对象使用介绍
- 安卓应用的版本更新
- 技术博客里的一句无关技术的帖子
- NSMutableAttributedString字体颜色等属性
- iOS之轻松上手block
- android布局--总结
- collectionviewcell的圆角和阴影问题
- Android两种杀掉进程方式总结(System.exit()和Process.killProcess())
- Android之 ListActivity 的使用和在 ListView中添加图片文字