.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;

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.

How LINQ and PLINQ load CPU

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).

Tesing prime numbers finding with LINQ and PLINQ

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.

LINQ and PLINQ performance graph

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

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    4 thoughts on “.NET Framework 4.0: Comparing LINQ and PLINQ performance

    • February 1, 2010 at 8:52 am

      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.

    • August 17, 2010 at 11:39 am

      Intresting information. Thanks

    • October 21, 2010 at 4:00 am

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

    • February 17, 2011 at 1:29 am

      Is using stopwatch a reliable way of measuring time on a multi core machine?

    Leave a Reply

    Your email address will not be published. Required fields are marked *