当要使用的呢?要使

2023-09-03 02:41:09 作者:深爱之人不挂嘴边只藏心里

修改其他选项和下面稍微延长的问题。

EDIT Additional options and a slightly extended question below.

考虑这个人为的和抽象类体的例子。这表明执行的迭代四种不同的方式。

Consider this contrived and abstract example of a class body. It demonstrates four different ways of performing a "for" iteration.

private abstract class SomeClass
{
    public void someAction();
}

void Examples()
{
    List<SomeClass> someList = new List<SomeClass>();

    //A. for
    for (int i = 0; i < someList.Count(); i++)
    {
        someList[i].someAction();
    }

    //B. foreach
    foreach (SomeClass o in someList)
    {
        o.someAction();
    }

    //C. foreach extension
    someList.ForEach(o => o.someAction());

    //D. plinq
    someList.AsParallel().ForAll(o => o.someAction());

编辑:的增加,从答案和研究一些选项

Addition of some options from answers and research.

    //E. ParallelEnumerable
    ParallelEnumerable.Range(0, someList.Count - 1)
        .ForAll(i => someList[i].someAction());

    //F. ForEach Parallel Extension
    Parallel.ForEach(someList, o => o.someAction());

    //G. For Parallel Extension
    Parallel.For(0, someList.Count - 1, i => someList[i].someAction())
}

我的问题有两个部分。我错过了一些显著的选择吗?哪个选项是最好的选择,考虑到可读性,但主要的表现?

My question comes in two parts. Have I missed some significant option? Which option is the best choice, considering readability but primarily performance?

请说明的 SomeClass的实现的复杂性,或的计数 someList 将影响这种选择。

Please indicate if the complexity of the SomeClass implementation, or the Count of someList would effect this choice.

编辑:随着选项,令人眼花缭乱,我不希望我的code被选择被宠坏。要将THRID部件添加到我的问题,如果我的列表可以是任何长度,我应该默认为一个平行的选择吗?

With such a dizzying array of options, I wouldn't like my code to be spoilt by choice. To add a thrid part to my question, If my list could be any length should I default to a parallel option?

作为一个稻草人。我怀疑过 SomeClass的的所有实现和 someList 选项 //Ë所有长度。 ParallelEnumerable 将提供最好的平均表现,给多处理器architechtures的prevalanece。我没有做任何的测试来证明这一点。

As a straw man. I suspect that over all implementations of SomeClass and all lengths of someList option //E. ParallelEnumerable would offer the best average performance, given the prevalanece of multi processor architechtures. I haven't done any testing to prove this.

请注意:平行扩展需要使用 System.Threading.Tasks 命名空间的

Note: The parallel extensions will require the use of the System.Threading.Tasks namespace.

推荐答案

选项A才真正有意义实现索引和只会高性能那些序列具有 O(1)查找时间。一般情况下,我会使用的foreach 和变型,除非你有特殊的逻辑。

Option A only really makes sense for sequences that implement indexing and will only be performant for those that have O(1) lookup time. Generally, I would use the foreach and variants unless you have special logic.

另外请注意,是特殊的逻辑像的for(int i = 1; I&LT; list.Count;我++)可以使用LINQ扩展方法来实现:的foreach(在sequence.Skip VAR项(1))

Also note, that "special logic" like for (int i = 1; i < list.Count; i++) can be implemented with Linq extension methods: foreach(var item in sequence.Skip(1)).

所以,一般preFER B经一个。

So, generally prefer B over A.

至于C:这可能会造成混淆的其他开发人员,如果他们不使用的功能性风格

As to C: This can be confusing for other developers if they aren't used to the functional style.

至于D:这取决于很多因素。我想简单的计算,你不想这样做 - 你只会真正从并行受益,如果循环体需要一段时间来计算

As to D: This will depend on a lot of factors. I guess for simple calculations, you don't want to do this - you will only really benefit from parallelization if the loop body takes a while to compute.