Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Parallel.ForEach and ParallelEnumerable.ForAll [duplicate]

I have a collection of Model classes and I wan't to Map each class individually to View Model classes.

I can use Parallel.ForEach or ParallelEnumerable.ForAll, so what is the difference between the 2, if any?

I couldn't find anything on MSDN.

like image 277
Stan R. Avatar asked Mar 23 '23 16:03

Stan R.


1 Answers

I can use Parallel.ForEach or ParallelEnumerable.ForAll, so what is the difference between the 2, if any?

Parallel.ForEach is the equivelent to using foreach in normal code.

The ParallelEnumerable.ForAll method is effectively the equivelent to List<T>.ForEach.

Both will perform and work similarly, though the ForAll method requires you to be using PLINQ already, as you need a ParallelEnumerable. Parallel.ForEach works directly off any IEnumerable<T>.

In general, I tend to prefer Parallel.ForEach, as the sole purpose of ForAll is to cause side effects. There are some disadvantages to this, as described in detail by Eric Lippert.

like image 159
Reed Copsey Avatar answered Apr 05 '23 22:04

Reed Copsey