9

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?

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:

C#: Any benefit of List<T>.ForEach(...) over plain foreach loop?

Community
  • 1
  • 1
Emil Lerch
  • 4,416
  • 5
  • 33
  • 43
  • 1
    You might find some answers here http://www.microsoft.com/downloads/details.aspx?familyid=C3EA8FB5-650D-434B-A216-7E54C53965D1&displaylang=en – Instance Hunter May 13 '10 at 16:21

2 Answers2

13

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.

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • I understand and agree with measurement of performance, but it's nice to have a general guideline to start with (like your comment about fast loop bodies). The downloads link from @Daniel Straight have some good general stuff as well. http://www.microsoft.com/downloads/details.aspx?familyid=C3EA8FB5-650D-434B-A216-7E54C53965D1&displaylang=en – Emil Lerch May 13 '10 at 17:29
  • +1 for referencing the Parallel Programming Coding Guidelines. – Steven May 13 '10 at 19:27
2

I would say to always stick with simple (i.e. regular foreach) and only implement more complex stuff (i.e. Parallel ForEach) when you have a measurable requirement to do so. So if you can prove that in a particular instance that the normal foreach isn't as fast as you need, and you can prove for that particular instance that the Parallel foreach will solve your problem, then use the Parallel.

Otherwise just keep it simple.

NotDan
  • 31,709
  • 36
  • 116
  • 156
  • 3
    I disagree with this, "as fast as you need" is not the same "as fast as the user would like". We should look at pallelizing things more frequently as the avg number of cores rises. – H H May 16 '10 at 21:23