Performance of PLINQ Queries

Parallel LINQ (PLINQ)
The main goal of the Parallel LINQ, or PLINQ is to execute LINQ to Objects queries in parallel, realizing the benefits of multithreading. Using PLINQ is simple, if you have to perform the same task on each element in a sequence, and those tasks are independent. If you need the result of one calculation step in order to find the next, PLINQ is not for you but many CPU intensive tasks can in fact be done in parallel. To tell the compiler to use PLINQ, you just need to call AsParallel and let PLINQ handle the threading.
The following samples demonstrate the performance of PLINQ queries for different scenarios:




































The result is shown below:

Without the AsParallel call, we would only use a single thread. Please note that except you specify that you want the results in the same order as the original sequence, PLINQ will assume you don’t mind getting results as soon as they’re available, even if results from earlier elements haven’t been returned yet. You can prevent this by using AsParallel().AsOrdered()

When to Use PLINQ

It’s tempting to search your existing applications for LINQ queries and experiment with parallelizing them. This is usually unproductive, because most problems for which LINQ is obviously the best solution tend to execute very quickly and so don’t benefit from parallelization. A better approach is to find a CPU-intensive bottleneck and then consider, “Can this be expressed as a LINQ query?”
PLINQ is well suited to embarrassingly parallel problems. It also works well for structured blocking tasks, such as calling several web services at once. PLINQ can be a poor choice for imaging, because collating millions of pixels into an output sequence creates a bottleneck. Instead, it’s better to write pixels directly to an array or unmanaged memory block and use the Parallel class or task parallelism to manage the multi-threading.

Copyright © All Rights Reserved - C# Learners