Showing posts with label parallel query. Show all posts
Showing posts with label parallel query. Show all posts

Saturday, December 11, 2010

Taking advantage of PLINQ feature in VS2010

Most of us have been installed Visual Studio 2010 and started using with .net framework 4.0. Definitely there are some new exciting features added with this framework and among PLINQ(Parallel LINQ) is one of the most interesting for developer to show faster development.
Nowadays its very common to use multi-core processor systems. PLINQ helps to utilize the multi-core facilities of your application.
PLINQ is LINQ executed in Parallel, that is, using as much processing power as you have in your current computer. For example having DUAL CORE(2 processor) will elimiante the processing time into half of the total time. Using "only" LINQ you won't get as much performance because the standard Language Integrated Query operators won't parallelize your code. That means your code will run in a serial fashion not taking advantage of all your available processor cores.

.NET framework 4.0 contains a new class ParallelEnumerable in the System.Linq namespace to split the work of queries across multiple threads.

Let me describe with simple example of code. We need a large collection to demonstrate the effect of parallel query otherwise the effect wont be visible if it fits in your system cache. Lets initiate the array first :

const int arraySize = 100000000;

var data = new int[arraySize];

var r = new Random();

for (int i = 0; i <>

{

data[i] = r.Next(40);

}


Now from the large array wewill filter data and get the sum of the filtered data. For instance we will filter it by the values greater than 20. We use the code below to get the filtered sum of the filtered data :

var sum = (from x in data.AsParallel()

where x <>

select x).Sum();

The only difference we see is the method AsParallel(). AsParallel() is defined with the ParallelEnumerable class to extend the IEnumerable <> interface, so it can be called with a simple array. AsParallel() returns ParallelQuery. This method will automatically manage to distribute your task make done form multiple processor.

We can compare the above process without using the AsParallel() method by taking the total time elapsed for both the process in the same computer.

You can see the time will dramatically decrease to half in a dual core machine, one fourth in quad-core machine if we use the AsParallel() method.

To know more about this topics check out the links below :

http://msdn.microsoft.com/en-us/library/dd997425.aspx

http://www.leniel.net/2009/11/parallel-linq-plinq-visual-studio-2010.html

http://geekdeck.com/plinq-c-example/


Rest Service using WEB API C#

This post will show a simple example of REST service built on Micrsoft WEB API framework. WEB API is an extensible framework from microsof...