.Net Framework 4.0: System.Linq.Parallel

No.of Views847
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  15 Feb 2010 23:02:11
Tag : ADO.NET , How to
.Net Framework 4.0: System.Linq.Parallel
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

.Net Framework 4.0 has parallel computing extensions for LINQ. Previously it was possible to download parallel extensions for LINQ separately from CodePlex. Of course, you can still use these extensions if you have older version that 4.0. I wrote a little, simple and pretty pointless example that illustrates how parallel queries work in .Net Framework 4.0.

My example creates simple data source that contains integers from 1 to 100. Then it queries this source two times. At first we will run query with parallel extensions and then we will run usual LINQ query. After querying we will write out the results. So it is really simple code but it returns some interesting results.

{codecitation class="brush: csharp; gutter: true;" width="500px"}



private static void ParallelAndSerialQueries()
{
// Source data that parallel and serial queries use.
var source = Enumerable.Range(1, 100);
 
// Perform parallel query
var parallelQuery = from p in source.AsParallel()
where p.ToString().Contains("1")
select p;
 
    parallelQuery.ForAll((x) =>
    {
Console.WriteLine("Parallel: " + x);
    });
 
// Perform serial query
var serialQuery = from s in source
where s.ToString().Contains("1")
select s;
    serialQuery.All((x) =>
    {
Console.WriteLine("Serial: " + x);
return true;
    });
 
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}

{/codecitation}

Output

Serial Parallel

1
10
11
12
13
14
15
16
17
18
19
21
31
41
51
61
71
81
91
100

10
11
1
14
12
13
16
17
18
19
21
31
15
51
61
41
71
81
100
91


Now let’s compare data that queries returned. We can see that in the case of classic serial LINQ query and data processing all the values are handled as they were sorted. Bigger number is always after smaller number. This is the same order as numbers have in our data source.

Parallel results are different. If we look at the results we can see that numbers are not ordered all the time. Some numbers are printed out much sooner or later than we may be expected before. This is because of the parallel nature of first query.

Parallel activities doesn’t run on same processor core. Some cores have more things to do and some cores have more free resources. That’s why parallel results have seemingly random order. Okay, their order is not random and as I just said it depends on workload of cores.

Is parallel processing more powerful?

Parallel computing is not another silver bullet that automagically solves performance issues. In the current example serial query was about two times faster that parallel one (exact numbers with 10000 elements in source: serial – 0.68 seconds, parallel – 1.31 seconds). Of course there are scenarios when parallel computing performs way better than serial computing. I recommend you to test the performance of your LINQ queries before you make your final decision over serial and parallel processing.

Thank you

Amal

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top