Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Parallel.Invoke and Parallel.ForEach essentially the same thing?

Tags:

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.

EDIT: I should have been clearer; I am asking if the two methods do the same thing because if they do not, I would like to understand which would be more efficient.

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?

like image 493
Brad Gagne Avatar asked Jun 03 '12 01:06

Brad Gagne


People also ask

What is parallel invoke?

Parallel.Invoke() simply executes a list of methods passed to it, in parallel: Example: static void Main(string[] args) { //This line will run the methods A,B,C,D and E in parallel.

Which is faster parallel ForEach or ForEach?

The execution of Parallel. Foreach is faster than normal ForEach.

Is parallel ForEach synchronous?

Parallel itself is synchronous. Parallel. ForEach is multiple threads solution while Task. WhenAll will probably share threads.

Is parallel ForEach blocking?

No, it doesn't block and returns control immediately. The items to run in parallel are done on background threads.


2 Answers

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

eg.

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

Parallel.Invoke can invoke many functions in parallel.

eg.

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

As from the example above, you can see that they are different in functionality. ForEach iterates through a List of elements and performs one task on each element in parallel, while Invoke can perform many tasks in parallel on a single element.

like image 162
Mayank Avatar answered Sep 19 '22 15:09

Mayank


Parallel.Invoke and Parallel.ForEach (when used to execute Actions) function the same, although yes one specifically wants the collection to be an Array. Consider the following sample:

List<Action> actionsList = new List<Action>             {                 () => Console.WriteLine("0"),                 () => Console.WriteLine("1"),                 () => Console.WriteLine("2"),                 () => Console.WriteLine("3"),                 () => Console.WriteLine("4"),                 () => Console.WriteLine("5"),                 () => Console.WriteLine("6"),                 () => Console.WriteLine("7"),                 () => Console.WriteLine("8"),                 () => Console.WriteLine("9"),             };              Parallel.ForEach<Action>(actionsList, ( o => o() ));              Console.WriteLine();              Action[] actionsArray = new Action[]             {                 () => Console.WriteLine("0"),                 () => Console.WriteLine("1"),                 () => Console.WriteLine("2"),                 () => Console.WriteLine("3"),                 () => Console.WriteLine("4"),                 () => Console.WriteLine("5"),                 () => Console.WriteLine("6"),                 () => Console.WriteLine("7"),                 () => Console.WriteLine("8"),                 () => Console.WriteLine("9"),             };              Parallel.Invoke(actionsArray);              Console.ReadKey(); 

This code produces this output on one Run. It's output is generally in a different order every time.

0 5 1 6 2 7 3 8 4 9

0 1 2 4 5 6 7 8 9 3

like image 44
plukich Avatar answered Sep 18 '22 15:09

plukich