.NET Framework 4.0: Comparing LINQ and PLINQ performance

In one of my sessions I demonstrated how PLINQ uses two CPU-s instead of one and has better performance than non-parallel processing. In this posting I will introduce my experiment and show you my example results. In the end of this posting you can find source code that is ready for demonstration.

I used prime numbers finding code as example and I want to say thanks for great example to keeper of Non-Destructive Me blog. You can find example code from his or her blog entryUsing PLINQ to Speed Up CPU Bound Operations – Demo. I modified demo code a little bit to fit my needs. In this posting I am using modified example. You can find link to my code from the end of this posting.

Demo code uses two methods to find prime numbers – LINQ and PLINQ. PLINQ – Parallel LINQ – is able to use all CPU-s available in machine. When you go through this demonstration you will see how easily you can move from LINQ to PLINQ.


PrimeNumberDemo.cs contains the following code block that measures time for LINQ and PLINQ version of prime numbers finding code. Leave this code as it is if you just want to see how much time one or another method takes.

//Run the demos for both parallel and sequential runs
long sequentialTime = RunSequentialTest(numbersToCheck);
long parallelTime = RunParallelTest(numbersToCheck);

If you want to get better idea what happens with CPU-s you should run LINQ and PLINQ version separately. Open Task Manager and make it stay always on top and make the following modifications to source code before running it.

If you want to see how LINQ works then use this code.

//Run the demos for both parallel and sequential runs
long sequentialTime = RunSequentialTest(numbersToCheck);
long parallelTime = 10000; //RunParallelTest(numbersToCheck);

If you want to see how PLINQ works then use this version of code.

//Run the demos for both parallel and sequential runs
long sequentialTime = 0; //RunSequentialTest(numbersToCheck);
long parallelTime = RunParallelTest(numbersToCheck);


If everything worked like expected then last two modifications to code should produce something like this in Task Manager.

You can see that LINQ uses resources of one CPU and it is therefore a little bit slower in our example. PLINQ uses both CPU-s at same time and performs faster. If you compare times you should get results similar to mine. Take a look at the following screenshot – click on it to resize it. Your results may be different and it depends on hardware of your computer (I expect you don’t have many background processes running).

When we put the results on chart we can see that there is not hard differences between LINQ and PLINQ when we don’t have much processing to do. But when our processes grow and need more resources then PLINQ performs very well.

Horizontal axis shows the maximum number we are walking to when finding prime numbers. Vertical axis shows time in milliseconds that LINQ (serial) and PLINQ (parallel) need to find prime numbers in this range.

If you want more comparisons then take a look at Stefan Cruysberghs blog posting .NET – Performance comparison with Parallel Extensions .NET 3.5.

Download sample solution

Liked this post? Empower your friends by sharing it!
Categories: .NET

View Comments (4)

  • This source code was a great example. So simple and easy to understand. Just one issue...
    In the function, RunParallelTest, there is the following code:
    var query = from num in numbersToCheck.AsQueryable()
    select new { Num = num, IsPrime = IsPrime(num.Value) };

    Tis isn't needed, is it? I commented it out, it built without errors and gave me the same results. Additionally, the serial and parallel functions are now identical except for the "foreach" line.

  • The first thing i find out this code which Matt talking about.
    I don't think that this code is codelines is useful.

Related Post