Free Information Technology Magazines and eBooks

Thursday, January 21, 2010

Parallel LINQ: Run Queries On Multi-Core Processors

Nowadays Multi-core processors setup is already a standard in servers and desktop PC. Its even already being used in mobile phones and PDAs which results to power consumption benefits. Microsoft Visual Studio Team Response to Multi-core environment is the enhanced version of LINQ, the Parallel Language Integrated Query (PLINQ). This feature is available in Visual Studio .NET 10. PLINQ is a query execution engine that can accept any LINQ-to-Objects or LINQ-to-XML query and automatically utilizes multiple cores for execution when they are available.


Take a look of this LINQ code, which use only 1 core on my quad core processor desktop pc.



IEnumerable arr = Enumerable.Range(1, 4000000);
var x =
from y in arr
where IsPrime(y)
select y.ToString();
List mylist = x.ToList();
Console.WriteLine(mylist.Count.ToString());


To take advantage of the other processors, we just have to change one line.



IEnumerable arr = Enumerable.Range(1, 4000000);
var x =
from y in arr.AsParallel()
where IsPrime(y)
select y.ToString();
List mylist = x.ToList();
Console.WriteLine(mylist.Count.ToString());


Isn't cool?

For More Coding Tips & Tricks, subscribe now.

No comments: