Converting a LINQ query to PLINQ

Posted by: Suprotim Agarwal , on 7/3/2011, in Category LINQ
Views: 54317
Abstract: PLINQ provides support for Parallel programming and is closely related to the Task Parallel Library. In very simple words, PLINQ enables a query to automatically take advantage of multiple processors. In this article, we will see a simple example of transforming a LINQ query to PLINQ.

PLINQ provides support for Parallel programming and is closely related to the Task Parallel Library. In very simple words, PLINQ enables a query to automatically take advantage of multiple processors.

In my 50 LINQ Examples, Tips and How To's article, a www.dotnetcurry.com reader Mike Long commented that he wanted to see a simple example of transforming a LINQ query to PLINQ. In this short article, I will demonstrate the same.

Let us first compare these 2 pieces of code:

Sequential LINQ

sequential-linq

Parallel LINQ

parallel-linq

In the query above, we first use Enumerable.Range method to generate a sequence of large integral numbers within a specified range. We then select and print numbers divisible by 1234567. We print the time elapsed using the Stopwatch class.

In the second query, as you can observe, it so easy to create a Parallel query. All we have to do is simply call AsParallel( ) on the data source i.e. on ‘rng’ and a sequential LINQ query is transformed into a parallel PLINQ query. This  enables parallel operations on the data. Internally the data source is partitioned and the query operates on each partition in parallel. In case parallelization is not possible, the query is executed sequentially.

Tip: You can print the Thread.CurrentThread.ManagedThreadId and observe the differences between the output. The ThreadID in case of parallel execution will be different.

OUTPUT

linq-to-plinq

On the same note, you may also want to see LINQ: Generate Odd Numbers using Parallel Execution

Give me a +1 if you think it was a good article. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Diego on Tuesday, July 24, 2012 12:54 PM
Good article :D
Comment posted by Ankur Khandelwal on Monday, October 29, 2012 3:00 AM
Aweee....!!!!!!
Comment posted by gkmaheshwari on Monday, March 4, 2013 6:54 AM
I execute above  codee , but found some mistake... i think so.


in parallel Linq
you use
            foreach (var v in query)
instead you have to do like
            foreach (var v in query1)

????
Comment posted by amin on Wednesday, August 14, 2013 1:41 PM
ffff
Comment posted by kapil on Wednesday, August 28, 2013 7:38 AM
means collection is divided in partions and query operater works on each partion in parallel.thanks
Comment posted by Parallel LINQ taking more time than sequential on Tuesday, September 24, 2013 10:45 AM
1234567
2469134
3703701
4938268
6172835
7407402
8641969
9876536
Time in sequetional Execution : 120
Time in Parallel Execution : 0
1234567
4938268
9876536
2469134
7407402
3703701
8641969
6172835
Time in Parallel Execution : 200
Press any key to continue . . .
Comment posted by venkateswarlu on Monday, October 28, 2013 2:14 PM
nice one

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel