Can I use Plinq in all Linq queries?

I read that PLinq will automatically use non-parallel Linq if it finds PLinq more expensive. So I thought, why not use PLinq for everything (when possible) and let runtime decide which one to use.

Applications will be deployed on multi-core servers, and I am ready to develop a bit more code to work with parallelism.

What are the default pitfalls of using plinq?

+16
c # linq plinq task-parallel-library
May 23 '10 at 21:55
source share
1 answer

One fall of the pit - you lose the ability to use order in sets.

Take the following code:

var results = new int { 0 ,1 ,2 ,3 }; var doSomethingSpecial = (from r in results.AsParallel() select r / 2).ToArray(); 

You cannot count on the results coming in order, so the result can be any permutation of the set. This is one of the biggest mistakes, in the sense that if you are dealing with ordered data, then you may lose performance advantages due to the cost of sorting.

Another problem is that you lose the ability to detect known exceptions. Thus, I could not catch the null pointer exception (without saying you should do this) or even catch the FormatException exception.

There are many reasons why you should not always use Plinq in all cases, and I highlighted one more. Do not read also "automatic use of non-parallel Linq", it can only handle barrier cases when the request is simple, or will be too complicated for parallel operation.

Always remember that the more PLINQ uses the more resources that you will consume on the server, which are removed from other working threads.

Resources

MSDN PLNQ Technical Documentation

Paul Kimmel on PLINQ

+9
May 23 '10 at 23:17
source share
— -



All Articles