Converting a LINQ query to PLINQ

Posted by: Suprotim Agarwal , on 7/3/2011, in Category LINQ
Views: 87035
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

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
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
Comment posted by javed husain on Wednesday, November 12, 2014 10:59 AM
sir app koi bhi topic likhe bo topic industry se related hana chahiye aur easy way