Parallel.ForEach与foreach的指导原则是什么?(What are the guidelines for Parallel.ForEach vs. foreach?)

系统教程 行业动态 更新时间:2024-06-14 17:04:03
Parallel.ForEach与foreach的指导原则是什么?(What are the guidelines for Parallel.ForEach vs. foreach?)

我很好奇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.

更多推荐

本文发布于:2023-04-24 21:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0a47296baa9c7c8da505dded51f1c7c0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指导原则   ForEach   Parallel   guidelines   foreach

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!