加快列表框插入时间
我需要将 950 个长度为 2500 个字符的字符串添加到列表框中。我下面使用的方法需要 2.5 秒,理想情况下需要在 500 毫秒以内完成。
Stopwatch sw = Stopwatch.StartNew();
listBox1.BeginUpdate();
listBox1.Items.AddRange(items.ToArray());
listBox1.EndUpdate();
sw.Stop();
优化插入时间的最佳方法是什么?
谢谢。
I need to add 950 strings that are 2500 characters in length to a listbox. The method I am using below takes 2.5 seconds and ideally it needs to happen in less then 500ms.
Stopwatch sw = Stopwatch.StartNew();
listBox1.BeginUpdate();
listBox1.Items.AddRange(items.ToArray());
listBox1.EndUpdate();
sw.Stop();
What would be the best way to optimize the insertion time?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试的一件事是将这一行:更改
为类似这样的内容:
这样,在将项目放入您的项目之前,您就不需要创建一个全新的数组(
ToArray()
调用)的开销。列表框
。One thing you could try is changing this line:
to something like this:
That way, you do not have the overhead of creating a whole new array (the
ToArray()
call) before putting the items into yourListBox
.列表框正在处理 2500 个字符。这就是缓慢的原因。所有这些数据,包括与数组的转换,在内存中都是微不足道的。因此,跳过 ToArray 步骤不会产生任何影响。如果您的用户必须水平滚动才能看到此信息,那么您很可能会陷入“慢”的境地。
如果没有,请考虑稍微重构一下。策略:仅输入在常规宽度列表框中可见的字符数(大约 100 个)。完整的字符串保留在幕后。
MyString = Ctype(ListBox.SelectedItem,TruncatedListItem).Content
祝你好运!
Listbox is dealing with 2500 characters. That is what is slow. All that data, including converting to/from arrays, is peanuts in memory. Hence skipping the ToArray step not making a difference. If your users have to scroll horizontally to see this info, chances are, you're stuck with 'slow.'
If not, consider refactoring a tiny bit. Strategy: only put as many characters - about 100 - as are viewable in a regular width listbox. Full strings are retained behind the scenes.
MyString = Ctype(ListBox.SelectedItem,TruncatedListItem).Content
Good luck!