大多数efficent的方式与IEnumerable的工作 [英] Most efficent way to work with IEnumerable

查看:227
本文介绍了大多数efficent的方式与IEnumerable的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是遍历集合/ IEnumeration C#中的最有效的方式。我有一个包含几乎1100对象列表。近10这些对象,inturn包含千子对象(的同一类型)。穿越到这个列表需要近5-6秒。这里是我的代码:

What is the most efficient way to traverse a collection/IEnumeration in C#. I have a list which contains almost 1100 objects. almost 10 of those objects, inturn contain 1000 subobjects (of same type). Traversal to this list takes almost 5-6 seconds. Here is my code:

foreach (Parameter par in this.AllParameters) //this.AllParameters is Generic.List type
{
    foreach (Parameter subPar in par.WrappedSubParameters)
    {
        subPar.IsSelected = false;
    }
    par.IsSelected = false;
}



有没有办法来优化这个代码,以便它足够快,不服用5-6秒?

Is there a way to optimize this code so that it is fast enough, not taking 5-6 seconds?

推荐答案

的循环,正如所写的,有可能的最快的选项之一。

The loops, as written, are likely one of the fastest options.

由于这是所有在内存中,每次写操作似乎是一个单独的实例(没有同步),你可能并行这得到一些收获:

Since this is all in memory, and each write operation appears to be on a separate instance (no synchronization), you could potentially parallelize this to get some gains:

Parallel.ForEach(this.AllParameters, par =>
{
    foreach (Parameter subPar in par.WrappedSubParameters)
    {
        subPar.IsSelected = false;
    }
    par.IsSelected = false;
});

请注意,我只并行外部循环(故意),因为这应该提供足够的工作。项目充分利用所有的处理核心。

Note that I'm only parallelizing the outer loop (on purpose), as this should provide enough work items to adequately use all processing cores.

另一个潜在的问题 - 如果你的 IsSelected 属性绑定到数据通过控制绑定,用户界面将可能被不断更新,这可以解释的很慢的更新时间。这将导致并行有没有真正的影响,同时,由于瓶颈不会是这些循环,而用户界面结合。

Another potential issue - if your IsSelected properties are bound to a control via Data binding, your UI will potentially be updating continually, which could explain the very slow update times. This would cause parallelization to have no real effect, as well, since the bottle neck would not be these loops, but rather the UI binding.

您可能要取消绑定/重新绑定控制,或者在控制暂停更新,直到你循环结束。

You may want to unbind/rebind the control, or suspend updates on the control until you're loop is completed.

这篇关于大多数efficent的方式与IEnumerable的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆