加快列表框插入时间

发布于 2024-09-12 13:12:43 字数 278 浏览 5 评论 0原文

我需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜扑 2024-09-19 13:12:43

您可以尝试的一件事是将这一行:更改

listBox1.Items.AddRange(items.ToArray());

为类似这样的内容:

foreach (var item in items)
{
    listBox1.Items.Add(item);
}

这样,在将项目放入您的项目之前,您就不需要创建一个全新的数组(ToArray() 调用)的开销。 列表框

One thing you could try is changing this line:

listBox1.Items.AddRange(items.ToArray());

to something like this:

foreach (var item in items)
{
    listBox1.Items.Add(item);
}

That way, you do not have the overhead of creating a whole new array (the ToArray() call) before putting the items into your ListBox.

空气里的味道 2024-09-19 13:12:43

列表框正在处理 2500 个字符。这就是缓慢的原因。所有这些数据,包括与数组的转换,在内存中都是微不足道的。因此,跳过 ToArray 步骤不会产生任何影响。如果您的用户必须水平滚动才能看到此信息,那么您很可能会陷入“慢”的境地。

如果没有,请考虑稍微重构一下。策略:仅输入在常规宽度列表框中可见的字符数(大约 100 个)。完整的字符串保留在幕后。

  • 像这样创建一个帮助器类(如果您是那个肛门者,您可以将公共字符串转换为属性;-):
    Class TruncatedListItem
        Public Content as string
        Overrides sub ToString() as string ' Pardon me if this is wrong I always use intellisense
            return Mid(Content,1,100)
        end sub
    end class
  • 将这些项目添加到列表框中。当列表框中的项目不是字符串类型时,它会调用该项目的 ToString 方法(嘿,我们只是为了给列表框一个休息而定制的)并将其添加为字符串,然后项目集合显示为您的项目我补充道。 (也保留开始/结束更新)
    对于 Items 中的每个 itm 作为字符串
        暗淡 tli 作为新的 TruncatedListItem
        tli.内容 = itm
        列表框.add(tli)
    next
    
    
  • 当你想查看用户选择的内容时,不要像这样获取字符串:
    MyString = Ctype(Listbox.SelectedItem,string) 
  • 执行此操作

    MyString = Ctype(ListBox.SelectedItem,TruncatedListItem).Content

  • 现在我假设用户在某些时候仍然需要在选择之前查看所有 2500 个字符。除非他们是一个严重的困境,否则他们应该接受这种替代方案(事实上,滚动有优点)。
    • 当他们双击某个项目时,在双击处理程序中,会在消息框中向他们显示完整文本。您可以在工具提示中告诉他们这样做。例如,在双击处理程序中: msgbox Ctype(ListBox.SelectedItem,TruncatedListItem).Content,,"Full Item Text"

祝你好运!

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.

  • Make a helper class like so (you can convert the public string to a property if you're that anal;-):
    Class TruncatedListItem
        Public Content as string
        Overrides sub ToString() as string ' Pardon me if this is wrong I always use intellisense
            return Mid(Content,1,100)
        end sub
    end class
  • Add those items to the listbox. When an item in a listbox isn't of type string, it calls the item's ToString method (which hey, we just tailored to give the listbox a break) and adds that as a string, then the items collection appears as the items you've added. (keep the begin/end update too)
    For each each itm as string in Items
        dim tli as new TruncatedListItem
        tli.Content = itm
        listbox.add(tli)
    next
  • When you want to see what the user picked, instead of getting the string like this:
    MyString = Ctype(Listbox.SelectedItem,string)  
  • do this

    MyString = Ctype(ListBox.SelectedItem,TruncatedListItem).Content

  • Now I am assumming the user, at some point, still needs to see all 2500 chars before selecting. Unless they are a serious stick in the mud, they should settle for this alternative (In fact there are advantages to scrolling).
    • When they double click an item, in the handler for double click, show them the full text in a messagebox. You could tell them to do that in a tooltip. For example, in the double-click handler: msgbox Ctype(ListBox.SelectedItem,TruncatedListItem).Content,,"Full Item Text"

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文