How to Improve Linq Query Performance by using Parallel/ PLinq
I want to highlight the disadvantage first, because if you know the disadvantage you can make a choice if required in your project or not.
Don't use if you have a Single core or dual core CPU's. I found that depending on the complexity of For loop the CPU performance reaches more than 70-80% (Not talking about the simple For loop). So if you want to use the parallelism for complex loop make sure you have more than 2 CPU's
Parallelization sometimes cause a PLinq query to run slower than Linq to object equivalent. Basically it said like queries with few data source elements and fast user delegates are reluctant to speed up much.
Parallel Linq is the parallel implementation of Linq. Parallel Linq implementation on loops provide better performance compared to sequential loop. Parallel Linq also works like Linq queries operate on any in-memory IEnumerale(of T) data source. PLINQ make full use of all processors on the system. The parallelism is achieved by diving the data source into multiple segments and working each segment on different worker threads on multiple processor.
MaxDegreeOfParallelism - Upper limit of number of processors irrespective of number of cores(We can only specify the maximum )limit.. So even if also if we don't use the CPU , no extra task will run in parallel.
Implementation
List testModelList=GetTestModelList();
Parallel.ForEach(testModelList,
new ParallelOptions { MaxDegreeOfParallelism = 5 },
tm=>; {//Do Operations
});
Reference:http://msdn.microsoft.com/en-us/library/dd460688.aspx
Comments
Post a Comment