158

Erg, I'm trying to find these two methods in the BCL using Reflector, but can't locate them. What's the difference between these two snippets?

A:

IEnumerable<string> items = ...

Parallel.ForEach(items, item => {
   ...
});

B:

IEnumerable<string> items = ...

foreach (var item in items.AsParallel())
{
   ...
}

Are there different consequences of using one over the other? (Assume that whatever I'm doing in the bracketed bodies of both examples is thread safe.)

0

4 Answers 4

159

They do something quite different.

The first one takes the anonymous delegate, and runs multiple threads on this code in parallel for all the different items.

The second one not very useful in this scenario. In a nutshell it is intended to do a query on multiple threads, and combine the result, and give it again to the calling thread. So the code on the foreach statement stays always on the UI thread.

It only makes sense if you do something expensive in the linq query to the right of the AsParallel() call, like:

 var fibonacciNumbers = numbers.AsParallel().Select(n => ComputeFibonacci(n));
Sign up to request clarification or add additional context in comments.

1 Comment

What's the benefit over simply doing a parallel foreach on the computefibonacci?
59

The second method will not be parallel the correct way to use AsParallel() in your example would be

IEnumerable<string> items = ...

items.AsParallel().ForAll(item =>
{
    //Do parallel stuff here
});

2 Comments

Why use the combination of asparallel along with forall instead of simply foreach?
Simply foreach wouldn't run parallel.
55

The difference is, B isn't parallel. The only thing AsParallel() does is that it wraps around a IEnumerable, so that when you use LINQ methods, their parallel variants are used. The wrapper's GetEnumerator() (which is used behind the scenes in the foreach) even returns the result of the original collection's GetEnumerator().

BTW, if you want to look at the methods in Reflector, AsParallel() is in the System.Linq.ParallelEnumerable class in the System.Core assembly. Parallel.ForEach() is in the mscorlib assembly (namespace System.Threading.Tasks).

2 Comments

What do you mean by ... Their parallel variants are used...?
@punctuation That, for example, when you write .Select(), it calls ParallelEnumerable.Select() and not the normal Enumerable.Select().
0

What's the difference between these two snippets?

The first snippet is parallel and the second is not.

  1. The first snippet invokes the ... code on parallel on multiple ThreadPool threads, including also the current thread as one of the worker threads.
  2. The second snippet invokes the ... code sequentially on the current thread only.

Enumerating the items.AsParallel() is practically the same as simply enumerating the items. Attaching the AsParallel PLINQ operator this way only serves at creating confusion to anyone reading the code. The AsParallel is intended as a starting point for attaching more PLINQ operators to the query. As a standalone operator it does nothing.

From the documentation:

ParallelEnumerable Operator Description
AsParallel The entry point for PLINQ. Specifies that the rest of the query should be parallelized, if it is possible.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.