- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这两种方法有什么区别?
一个比另一个更有效率吗?
我在想 AppendText() 可能使用类似于 StringBuilder 的方法,即它使用自己的缓存而不是每次都创建和附加一个新字符串,是这样吗?
谢谢。
最佳答案
正如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.
你的问题,
what is the difference between these two methods?
我们都知道 TextBox.Text += something;
是如何工作的,即每次创建和附加一个新字符串但是 AppendText
是如何工作的我找不到任何代码片段它在内部是否使用 StringBuilder
或其他东西。
Is one more efficient than the other?
我认为上述问题的答案将视情况而定,(基于测试用例观察)
if
Multiline
property is set tofalse
then Concatenation (+=) yields better results but on other handMultiline
property is set totrue
thenAppendText
yields far better performance.
编辑 阅读 comment from Rawling 后我制作了一个自定义的 win-form 解决方案,其中我有一个简单的 textbox
,我在其中附加了一个简单的字符串 hello
10000 次,使用一个简单的 for循环
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:多行属性为真我不得不将迭代次数减少到一半,即 5000 次用于文本连接,因为这需要很长时间。
btnAppendText_Click
output ontxtTime
was37222129
almost 3-4 seconds for 10000 iterationbtnConcante_Click
output ontxtTime
was14449906487
more than 25 minutes for only 5000 iterations.
从上面的结果很明显,AppendText
比 更快更有效(当
Multiline
为 true
时) >串联
测试 2:多行属性为假
btnConcante_Click
output ontxtTime
was39862280
almost 3-4 seconds for 10000 iterationbtnAppendText_Click
output ontxtTime
was1043279672
almost 2-3 minutes for 10000 iteration
从上面的结果可以明显看出,Concatenation 比 AppendText
更快更有效(当 Multiline
为 false
时)
关于c# - TextBox.Text += "string";与 TextBox.AppendText ("string");,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20632372/
这个问题在这里已经有了答案: How to automatically select all text on focus in WPF TextBox? (33 个答案) How to select
我在我的项目中使用了以下模板:
我有一个非常奇怪的问题。我有多个用于用户名/密码的文本框,每个用户/密码组旁边都有一个复选框。当用户单击 CheckBox 时,如果他选中它,它旁边的用户名和密码 TextBox 将被启用,焦点将设置
我有两个带有这样的日期选择器的文本框。 $(function () { $('#value_date').datetimepicker({ format: 'MM/DD/YY
我检查了大约 60 个国家/地区的邮政编码。用户输入邮政编码,根据该国家/地区的规则进行检查。美国是个异常(exception),我可以根据已输入的其他街道地址信息自动填写值。在某些方面,它基本上是一
我们的应用程序启动几个后台进程并将它们的输出放入 TextBoxes - 每个在 TabControl 中的一个单独的 TabItem。我希望 TextBoxes 自动滚动以显示最后一个输出行,因此在
TextBlock 元素很好地处理了 LineHeight,允许文本完全显示,而无需剪切。但是,我想将其更改为 TextBox 以方便编辑文本,这就是我的麻烦开始的地方。 TextBlock 显示如下
我正在尝试“帮助”用户表单上文本输入框的用户输入百分比。 截至目前,我的输入框将任何输入转换为百分比形式。这意味着键入“6”会将值转换为 600%。如下所示 databaseViewer 是输入框从中
我有一个带有 RichEditBox 的 Windows 应用商店应用程序(编辑)和一个 Grid (边注)。 我需要始终匹配两个元素的垂直滚动位置。这样做的目的是允许用户在文档的页边空白处添加注释。
我有一个问题。下面的代码用于通过 .append() 和 .remove() 动态添加和删除文本框。我希望占位符为 textbox 的文本框中的所有数据都会内爆,并放置在我设置的名称为 textbox
我有一个文本框,其 Text 属性具有 TwoWay MultiBinding,UpdateSourceTrigger 设置为 属性已更改。第一个 Binding 是一个依赖属性 (Value),它具
它们之间有什么区别吗?谢谢 TextBox.Clear(); TextBox.Text = string.Empty; 最佳答案 在实践中:不。在内部,有。两者都以完全不同的方式清除文本。 我不敢告
这两种方法有什么区别? 一个比另一个更有效率吗? 我在想 AppendText() 可能使用类似于 StringBuilder 的方法,即它使用自己的缓存而不是每次都创建和附加一个新字符串,是这样吗?
我有一个文本框来显示一个很长的 Base64 字符串。 TextBox.Multline = true 和 TextBox.WordWrap = true。 此问题是由文本框本身的自动字边界检测引起的
在 this document在描述 Windows 10 UWP 应用程序的生命周期时,它指出: Users now expect your app to remember its state as
我需要检查如果我的 TextBox (txtmoney) 为空或等于 0,则不允许按 0 键键盘或数字 0。 我该怎么做? 最佳答案 Before implementing please refer
我有一个显示一些 TextBoxes 的 ListView。对于每个 TextBox,我都会捕获一个 SelectionChanged 事件。 我的 XAML 文件看起来像这样:
当我单击(或双击)Access 表单的文本框时,我需要选择其中的所有文本。我尝试了以下代码,但没有成功: Me.txt_CompraPreco.SelStart = 0 Me.txt_CompraPr
我有一个 ListView(在更新面板中)绑定(bind)到一个 ObjectDataSource(连接到一个 NHibernate 对象)并且在 EditItemTemplate 我使用 Text=
我正在尝试使用 D3 绘制多条交互式线条。单击每一行时,应根据当前鼠标坐标显示一个 float 文本框,并等待输入更新行坐标。更改文本框后,该行应自动更新,文本框应消失。有人可以看看我的代码并找出问题
我是一名优秀的程序员,十分优秀!