PLINQ Powered by TPL in .Net 4.0 C#

My previous post discussed the TPL library and its uses to create scalable applications. Another way to incorporate the parallel task into your .Net applications is through PLINQ.

 
LINQ queries that are designed to run in Parallel are termed 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 it's likely to benefit from parallelization when run concurrently.
 
The TPL library provides the following methods to support the parallel LINQ queries.
 
AsParallel()
 
Specifies that the rest of the query should be parallelized, if possible.
 
WithCancellation()
 
Specifies that PLINQ should periodically check for the state of the provided CancellationToken and cancel execution if it is required.
 
WithDegreeOfParallelism()
 
Specifies the maximum number of processors that PLINQ should use to parallelize the query.
 
ForAll()
 
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's see the demo:
 
Creating the first PLINQ query:
 
If you want to use TPL to execute your query in parallel (if possible), then you will want to use the AsParallel() extension method:
  1. //Prepare the query  
  2. int[] modeThreeIsZero = (from num in source.AsParallel()  
  3.                          where (num % 3) == 0  
  4.                          orderby num descending  
  5.                          select num).ToArray(); 
Here is the complete code:
  1. namespace PLINQ_Demo {  
  2.     class Program {  
  3.         static DateTime startTime;  
  4.         static void Main(string[] args) {  
  5.             ProcessIntDataNormalMode();  
  6.             ProcessIntDataNormalMode();  
  7.             Console.ReadLine();  
  8.         }  
  9.   
  10.         private static void ProcessIntDataNormalMode() {  
  11.             //record current time  
  12.             startTime = DateTime.Now;  
  13.   
  14.             //Get a random large array of integers  
  15.             int[] source = Enumerable.Range(1, 10000000).ToArray();  
  16.   
  17.             //Prepare the query  
  18.             int[] modeThreeIsZero = (from num in source.AsParallel() where(num % 3) == 0 orderby num descending select num).ToArray();  
  19.             //timer  
  20.             TimeSpan ts = DateTime.Now.Subtract(startTime);  
  21.   
  22.             //Process and show the result  
  23.             Console.WriteLine("{0} numbers found as result. \n Time Elapsed: {1} Seconds:MilliSeconds in Normal mode", modeThreeIsZero.Count(), ts.Seconds + ":" + ts.Milliseconds);  
  24.         }  
  25.   
  26.         private static void ProcessIntDataParallelMode() {  
  27.             //record current time  
  28.             startTime = DateTime.Now;  
  29.   
  30.             //Get a random large array of integers  
  31.             int[] source = Enumerable.Range(1, 10000000).ToArray();  
  32.   
  33.             //Prepare the query  
  34.             int[] modeThreeIsZero = (from num in source.AsParallel() where(num % 3) == 0 orderby num descending select num).ToArray();  
  35.   
  36.             //timer  
  37.             TimeSpan ts = DateTime.Now.Subtract(startTime);  
  38.             //Process and show the result  
  39.             Console.WriteLine("{0} numbers found as result \n Time Elapsed: {1} [Seconds:MilliSeconds] in parallel mode.", modeThreeIsZero.Count(), ts.Seconds + ":" + ts.Milliseconds);  
  40.         }  
  41.     }  
Output
 
TPL


Similar Articles