Johan Åhlén

Johan Åhlén's blog about life, universe and everything.

Surprising parallel processing performance in .NET 4.0

C# and .NET Framework 4.0 has a good number of really good new features. Some of the best features, from a performance point of view, is the Task Parallel Library and Parallel LINQ (PLINQ). I did some performance testing recently and was surprised by the results.

Here's my test code:

// Create an array of a million random numbers
Random rand = new Random(0);
var randomNumbers = Enumerable.Range(1, 1000000).Select(i => rand.Next(1000000)).ToArray();

// Sort the numbers. Use stopwatch to measure.
var stopWatch = Stopwatch.StartNew();
var sortedNumbers = randomNumbers.OrderBy(i => i).ToArray();

// Display results
Console.WriteLine("Time: {0:F6} seconds", stopWatch.Elapsed.TotalSeconds);

This takes approx 1.4 seconds to execute on my machine (a laptop with a dual-core Intel CPU).

So, could sorting be done in parallel? Does PLINQ contain a parallel sorting algorithm that would make use of both cores? How much faster would that algorithm be? Twice as fast?

To test this I changed one line:

var sortedNumbers = randomNumbers.AsParallel().OrderBy(i => i).ToArray();

Now, how fast do you think it was? On my machine it took approx 0.5 seconds. That means it not only makes use of both cores, but it must be a more clever algorithm. Very interesting for performance optimization.

On other operations such as filtering, I've not got this level of performance gain. Instead it reduced execution time by about 30%.

Even more surprising. On my example, the Sum() method is a little slower in PLINQ than sequential LINQ. So it seems you have to check every method if you want to ensure you get maximum performance.

Here's a summary of the new features in .NET Framework 4.0:

There's also a good summary of the C# 4.0 news in this blog post:




No Comments