PLINQ - Parallel LINQ Queries and TPL in C# .Net 4.0

In previous post Parallel programming using TPL libaray in .Net 4.0 we discussed about the TPL library and its uses to create the scalable applications. Another way to incorporate the parallel task into your .Net applications is through PLINQ.

LINQ queries which are designed to run in Parallel are termed as PLINQ queries.

The framework of PLINQ has been optimized in such a way that it includes determining if a query can perform faster in a synchronous manner. Is analyze the Linq query at run time if its likely to benefit from parallelization, when run concurrently.

The TPL library provides the following methods to support the parallel Linq queries.


Specifies that the rest of the query should be parallelized, if possible.


Specifies that PLINQ should periodically check for the state o fthe provided CancellationToken and cancel execution if it is required.


Specifies the maximum number of processors that PLINQ should use to parallelize the query.


Enables results to be processed in parallel without first merging back to the consumer thread, as would be the case when enumerating a LINQ result using the foreach keyword.

Let see the demo
Creating first PLINQ query:

if you want to use TPL to execute your query in parallel(if possible), you will want to use the AsParallel() extension method:

    1. int[] modeThreeIsZero = (from num in source.AsParallel()
    2.                                      where (num % 3) == 0
    3.                                      orderby num descending
    4.                                      select num).ToArray();

Here is the complete code to process and random array and analyze the Execution time:

  1. namespace PLINQ_Demo
  2. {
  3.     class Program
  4.     {
  5.         static DateTime startTime;
  6.         static void Main(string[] args)
  7.         {
  8.             ProcessIntDataNormalMode();
  9.             ProcessIntDataParallelMode();
  10.             Console.ReadLine();
  11.         }
  13.         private static void ProcessIntDataNormalMode()
  14.         {
  15.             //record current time
  16.             startTime = DateTime.Now;
  18.             //Get a random large array of integers
  19.             int[] source = Enumerable.Range(1, 10000000).ToArray();
  21.             //Prepare the query
  22.             int[] modeThreeIsZero = (from num in source.AsParallel()
  23.                                      where (num % 3) == 0
  24.                                      orderby num descending
  25.                                      select num).ToArray();
  26.             //timer
  27.             TimeSpan ts = DateTime.Now.Subtract(startTime);
  29.             //Process and show the result
  30.             Console.WriteLine("{0} numbers found as result. \n Time Elapsed: {1} Seconds:MilliSeconds in Normal mode", modeThreeIsZero.Count(), ts.Seconds + ":" + ts.Milliseconds);
  31.         }
  33.         private static void ProcessIntDataParallelMode()
  34.         {
  35.             //record current time
  36.             startTime = DateTime.Now;
  38.             //Get a random large array of integers
  39.             int[] source = Enumerable.Range(1, 10000000).ToArray();
  41.             //Prepare the query
  42.             int[] modeThreeIsZero = (from num in source.AsParallel()
  43.                                      where (num % 3) == 0
  44.                                      orderby num descending
  45.                                      select num).ToArray();
  47.             //timer
  48.             TimeSpan ts = DateTime.Now.Subtract(startTime);
  49.             //Process and show the result
  50.             Console.WriteLine("{0} numbers found as result \n Time Elapsed: {1} [Seconds:MilliSeconds] in parallel mode.", modeThreeIsZero.Count(), ts.Seconds + ":" + ts.Milliseconds);
  51.         }
  52.     }
  53. }


Let my code to choose if CPU cores can be uitilized!

No comments:

Post a Comment