什么是对的foreach的指导方针Parallel.ForEach?指导方针、foreach、ForEach、Parallel

2023-09-05 00:01:07 作者:思念在发烫

我很好奇Parallel.ForEach的性能特点。给定一个Parallel.ForEach循环内的任何有效结构,是它总是preferable使用Parallel.ForEach在foreach循环?我特别想知道调用的并行任务库小套或其它边缘情况下,foreach循环可能会更快的开销。我知道图书馆是pretty的智能何时/如何生成线程...在那里情况下,最好是见好就收code在foreach循环,或者是开销调用并行任务通常negligble,所以如果可以的话,你应该使用Parallel.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?

这个问题是相似的,并提供良好的功能上的差异信息,但并没有真正说话的表现。请注意,我忽略兼容.NET<为理由,住在一个foreach 4:

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:

http://stackoverflow.com/questions/1924535/c-any-benefit-of-listt-foreach-over-plain-foreach-loop

推荐答案

这并不总是preferable。对于快速循环体,Parallel.ForEach会降低性能。其中的Parallel编程编码准则是的测量的前后并行。

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.