C# - 是Parallel.Invoke和Parallel.ForEach本质上是一回事吗? [英] C# - Are Parallel.Invoke and Parallel.ForEach essentially the same thing?

查看:617
本文介绍了C# - 是Parallel.Invoke和Parallel.ForEach本质上是一回事吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和以同样的事情,我的意思是做这两个操作基本上做同样的工作,它只是归结为哪一个更方便的依据是什么,你有工作,打电话? (即委托列表或一个事物的列表遍历)?我一直在寻找的MSDN,计算器,以及各种随机文章,但我还没有找到一个清晰的答案。

And by "same thing" I mean do these two operations basically do the same work, and it just boils down to which one is more convenient to call based on what you have to work with? (i.e. a list of delegates or a list of things to iterate over)? I've been searching MSDN, StackOverflow, and various random articles but I have yet to find a clear answer for this.

编辑: 我本来应该更清晰;我问,如果这两种方法做同样的事情,因为如果他们不这样做,我想知道这将是更有效的。

例如:我有500键值的列表。目前我通过列表(连续)使用foreach循环迭代,并且对每一个项目进行工作。如果我想利用多核心的优势,我应该简单地使用Parallel.ForEach呢? 我们先来说说观点的缘故,我有500名代表对那些500任务的阵列 - ?会的净影响有什么不同要求Parallel.Invoke,给它的500名代表的名单

Example: I have a list of 500 key values. Currently I use a foreach loop that iterates through the list (serially) and performs work for each item. If I want to take advantage of multiple cores, should I simply use Parallel.ForEach instead? let's say for arguments's sake that I had an array of 500 delegates for those 500 tasks - would the net effect be any different calling Parallel.Invoke and giving it a list of 500 delegates?

提前感谢!

推荐答案

Parallel.ForEach 经历元素的列表,可以对数组的元素执行某些任务。

Parallel.ForEach goes through the list of elements and can perform some task on the elements of the array.

如。

Parallel.ForEach(val, (array) => Sum(array));

Parallel.Invoke 可并行调用许多功能。

Parallel.Invoke can invoke many functions in parallel.

如。

Parallel.Invoke(
() => doSum(array),
() => doAvg(ayyay),
() => doMedian(array));

由于从上面的例子可以看出,他们是在functionallity本质的区别。人们可以通过列表元素的迭代以及执行做平行的工作每一个元素,其他的可以调用很多的工作的并行单个元素。

As from example above you can see that they are essentially different in functionallity. One can Iterate through a List of elements and perform do parallel task on each element, other can invoke many tasks on single element in parallel.

这篇关于C# - 是Parallel.Invoke和Parallel.ForEach本质上是一回事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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