什么是对的foreach的指导方针Parallel.ForEach? [英] What are the guidelines for Parallel.ForEach vs. foreach?

查看:144
本文介绍了什么是对的foreach的指导方针Parallel.ForEach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<一个href="http://stackoverflow.com/questions/1924535/c-any-benefit-of-listt-foreach-over-plain-foreach-loop">http://stackoverflow.com/questions/1924535/c-any-benefit-of-listt-foreach-over-plain-foreach-loop

推荐答案

这并不总是preferable。对于快速循环体,Parallel.ForEach会降低性能。其中的<上市准则href="http://services.social.microsoft.com/feeds/FeedItem?feedId=639a99a9-ff25-4062-b61d-a86ea9d66a06&itemId=1b21d73b-64ca-40a3-b223-9d6190af5943&title=Parallel+Programming+in+.NET+4%3a+Coding+Guidelines&uri=http%3a%2f%2fdownload.microsoft.com%2fdownload%2fB%2fC%2fF%2fBCFD4868-1354-45E3-B71B-B851CD78733D%2fParallelProgramsinNET4_CodingGuidelines.pdf&k=IEQ0nRkCa5MddZRhy8uBw6rsQhbjXbYDr2e%2fCWtl0OA%3d">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.

这篇关于什么是对的foreach的指导方针Parallel.ForEach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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