.Net C# parallel Linq (PLINQ) and degree of parallelism example

A quick demo to see how the degree of parallelism affects the execution speed of a simple Linq/PLINQ query. The results indicate that using PLINQ could be more efficient if the executing environment has multiple cores.

using System;
using System.Diagnostics;
using System.Linq;

namespace CsharpPLinqDemo
{
    class CsharpPLinqExample
    {
        static void Main()
        {
            for (var i = 1; i <= 1; i++)
            {
                var myRange = Enumerable.Range(1, 1000000);

                Console.WriteLine("Processing..");

                var stopwatch = Stopwatch.StartNew();

                var result = myRange.Select(x => x);

                stopwatch.Stop();

                Console.WriteLine("Time: {0:FFFFFFF}", stopwatch.Elapsed);

                myRange = null;
                result = null;
            }

            Console.WriteLine();

            Console.WriteLine("Parallel Processing..");

            for (var i = 1; i <= Environment.ProcessorCount; i++)
            {
                var myRange = Enumerable.Range(1, 1000000);

                var stopWatch = Stopwatch.StartNew();

                var result = myRange.AsParallel()
                        .WithDegreeOfParallelism(i)
                        .Select(x => x);

                stopWatch.Stop();

                Console.WriteLine("Number of cores: {0} Time: {1:FFFFFFF}", i, stopWatch.Elapsed);

                myRange = null;
                result = null;
            }

            Console.WriteLine();

            Console.WriteLine("Processing and calling .ToList()");

            for (var i = 1; i <= Environment.ProcessorCount; i++)
            {
                var myRange = Enumerable.Range(1, 1000000);

                var stopWatch = Stopwatch.StartNew();

                var result = myRange.AsParallel()
                       .WithDegreeOfParallelism(i)
                       .Select(x => x).ToList();

                stopWatch.Stop();

                Console.WriteLine("Number of cores: {0} Time: {1:FFFFFFF}", i, stopWatch.Elapsed);

                myRange = null;
                result = null;
            }

            Console.WriteLine();

            Console.WriteLine("Processing and calling .ToList() after PLINQ execution");

            for (var i = 1; i <= Environment.ProcessorCount; i++)
            {
                var myRange = Enumerable.Range(1, 1000000);

                var stopWatch = Stopwatch.StartNew();

                var result = myRange.AsParallel()
                       .WithDegreeOfParallelism(i)
                       .Select(x => x);

                result.ToList();

                stopWatch.Stop();

                Console.WriteLine("Number of cores: {0} Time: {1:FFFFFFF}", i, stopWatch.Elapsed);

                myRange = null;
                result = null;
            }


            Console.ReadKey();
        }
    }
}

Results:

Processing..
Time: 0026234

Parallel Processing..
Number of cores: 1 Time: 0025167
Number of cores: 2 Time: 0000022
Number of cores: 3 Time: 0000022
Number of cores: 4 Time: 0000019

Processing and calling .ToList()
Number of cores: 1 Time: 1007239
Number of cores: 2 Time: 0862795
Number of cores: 3 Time: 0785533
Number of cores: 4 Time: 0543469

Processing and calling .ToList() after PLINQ execution
Number of cores: 1 Time: 0890755
Number of cores: 2 Time: 0567519
Number of cores: 3 Time: 0545084
Number of cores: 4 Time: 0473126

2 thoughts on “.Net C# parallel Linq (PLINQ) and degree of parallelism example”

  1. Interesting.

    Looks like ToList() might not support parallelism. Did you investigate further?

    So where do the threads to do this come from? If from the general thread pool then in a high-traffic website would there be contention for threads between the ASP.NET process and the use of PLINK in app code? How would you decide whether bespoke parallelism in such an app is overall beneficial vs. letting ASP.NET do its’ thing on the assumption that the Microsoft IIS, ASP.NET and perf guys know what they’re doing?

  2. A very good question, the ‘2 Minutes’ posts are really a quick summary of something I’ve been looking at. In regards to ToList() and parallelism this should be fine, however there are several cases where a parallel query may execute sequentially. The following articles should help:

    PLINQ Queries That Run in Parallel in .NET 4.5
    http://blogs.msdn.com/b/pfxteam/archive/2011/11/11/10235999.aspx

    PLINQ Queries That Run Sequentially
    http://blogs.msdn.com/b/pfxteam/archive/2009/10/31/9915569.aspx

    Your final point leads to a very good question, my response would be to measure everything and assume nothing! My take on performance is that it’s very much the responsibility of the developer/architect.

Leave a Reply

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