Quantcast
Channel: Tallan's Technology Blog » jraymunt
Viewing all articles
Browse latest Browse all 4

Intro to PLINQ

$
0
0

I recently had the opportunity to attend the Microsoft launch event for the new Visual Studio 2010. I was impressed with everything 2010 is bringing to the table, but what really caught my eye is the new Parallel FX library. Parallel FX provides developers with a set of tools which promises faster and less error prone development of multi-threaded applications. For this post I’m going to focus in on the new parallel LINQ, also known as PLINQ (which reminds me of Plinko every time I hear it).

For those of you who aren’t familiar with LINQ, I’ll try to sum it up in a few sentences. LINQ stands for language-integrated query. It provides .Net developers with a new syntax which makes performing set based operations on a collection, or multiple collections easier. For example, if we have an unordered list of integers which we need to sort, in the past we would have to do something similar to this:

public void RunSort()
{
  var integerList = new List<int> { 5, 63, 23, 1, 0, 91 };
  QuickSort(0, integerList.Count, integerList);
}

public void QuickSort(int left, int right, IList<int> integerList)
{
  var pivot = integerList[left];

  while (left < right)
  {
    while ((integerList[right] >= pivot) && (left < right))
    {
      right--;
    }

    if (left != right)
    {
      integerList[left] = integerList[right];
      left++;
    }

    while ((integerList[left] <= pivot) && (left < right))
    {
      left++;
    }

    if (left == right) continue;
    integerList[right] = integerList[left];
    right--;
  }

  integerList[left] = pivot;
  pivot = left;

  if (left < pivot)
  {
    QuickSort(left, pivot - 1, integerList);
  }

  if (right > pivot)
  {
    QuickSort(pivot + 1, right, integerList);
  }
}

The above example works fine, however as a developer we needed to make the decision of what sorting algorithm to use, and then go ahead and implement the sorting function. This wastes time and can sometimes be error prone. LINQ can solve this problem, and many other set based problems like it quickly and efficiently. Here is an example of the first example re-written to make use of LINQ:

public void RunSort()
{
    var integerList = new List { 5, 63, 23, 1, 0, 91 };
    var sortedIntegerList = from integer in integerList
                            orderby integer ascending
                            select integer;
}

LINQ takes care of the sorting here for us and provides a nice, easy to read syntax. When using LINQ, we can worry less about the details of working with data sets, and focus more about the business rules we are implementing. PLINQ takes LINQ a step further and extends this powerful set based functionality to take advantage of multi-core CPU’s. Here is an example of the second example converted to use PLINQ:

public void RunSort()
{
    var integerList = new List { 5, 63, 23, 1, 0, 91 };
    var sortedIntegerList = from integer in integerList.AsParallel()
                            orderby integer ascending
                            select integer;
}

When comparing the above example with the second example, only a couple of things have changed. One is obvious just by looking at the code. We are now calling the extension method .AsParallel on the list of integers. This tells PLINQ that it is OK to go ahead and try to parallelize the LINQ query. The second is a little more subtle. Instead of the LINQ query returning an IEnumerable type, it is returning an IParallelEnumerable. Luckily, IParallelEnumerable derives from IEnumerable, so existing code should work fine with this change.

This is of course a very basic example of how LINQ and PLINQ differ. But hopefully I’ve sparked some interest.

-Jon


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images