我很好奇Parallel.ForEach的性能特点。 给定一个Parallel.ForEach循环中的任何有效构造,是否最好在一个foreach循环中使用Parallel.ForEach? 我特别想知道在小集或其他foreach循环可能更快的边界情况下调用Parallel Tasks库的开销。 我知道这个库在何时/如何产生线程方面非常聪明......是否存在将代码留在foreach循环中更好的情况,或者调用Parallel Tasks的开销通常可以忽略不计,所以如果可以的话,您应该使用Parallel.ForEach?
这个问题是相似的,并提供良好的功能差异信息,但并不真正说话的表现。 请注意,我忽略了与.NET <4的兼容性,作为保留foreach的原因:
C#:List <T> .ForEach(...)在普通的foreach循环中的好处吗?
I'm curious about the performance characteristics of Parallel.ForEach. Given any valid construct inside a Parallel.ForEach loop, is it always preferable to use Parallel.ForEach over a foreach loop? I'm specifically wondering about the overhead of invoking the Parallel Tasks library on small sets or other edge cases where a foreach loop might be faster. I know the library is pretty smart about when/how to spawn threads...are there cases where it's better to just leave code in a foreach loop, or is the overhead for calling Parallel Tasks generally negligble, so if you can, you should use Parallel.ForEach?
This question is similar and provides good functional difference information but doesn't really speak to performance. Note that I'm ignoring compatibility to .NET <4 as a reason for staying with a foreach:
C#: Any benefit of List<T>.ForEach(...) over plain foreach loop?
最满意答案
这并不总是可取的。 对于快速循环体,Parallel.ForEach可能会降低性能。 并行编程编码指南中列出的准则之一是在并行化之前和之后测量 。
其他有用的文章已由并行计算组发布 。
It's not always preferable. For fast loop bodies, Parallel.ForEach can degrade performance. One of the guidelines listed in Parallel Programming Coding Guidelines is to measure both before and after parallelization.
Other useful articles have been published by the parallel computing group.
更多推荐
发布评论