Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

.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 entry Using 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.

Demo

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


Results

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

LINQ vs PLINQ

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

Results of 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

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.

Source code

Click here to download VS2010B2 solution with demo code.


kick it on DotNetKicks.com pimp it 顶 Progg it Shout it
Shout it!

Comments

SMiGL said:

Intresting information. Thanks

# February 1, 2010 3:52 PM

Matt Campbell said:

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 7:39 AM

Alyaksei said:

The first thing i find out this code which Matt talking about.

I don't think that this code is codelines is useful.

# October 21, 2010 12:00 AM

SS said:

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

# February 17, 2011 8:29 AM