
The Institute for Cyber Security uses C# and the .NET Framework for our startups in incubation, internal research projects, community exercises, and more. With the second beta of Visual Studio 2010 out, we've been really interested in the framework and language changes, and are evaluating it for integration into our software process. One change in particular is PLINQ, which stands for Parallel Language Integrated Query. This builds on LINQ, introduced two years ago, which added native data querying capabilities to .NET languages. Thus, today's Edify Friday covers a simple way to use PLINQ in C# code.
First, let's take a look at a simple LINQ example. Below is a simple program that counts the number of even numbers up to a ceiling (see Listing 1 below):
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
namespace PlinqExample {
class HelloWorldPlinq {
static void Main(string[] args) {
Stopwatch sw = Stopwatch.StartNew();
int numEven = CountEven(1000000);
Console.Write("Found {0} even numbers. ", numEven);
Console.WriteLine("Elapsed: {0} ms", sw.ElapsedMilliseconds);
}
private static int CountEven(int top) {
IEnumerable<int> allNumbers = Enumerable.Range(1, top);
var q = from i in allNumbers where (i % 2 == 0) select i;
return q.Count();
}
}
}
Listing 1: HelloWorldPlinq.cs
When running the application, one should get something similar to the following: "Found 500000 even numbers. Elapsed: 18 ms." (naturally, the elapsed time will vary based on CPU power and processor availability).
Now, we'll modify one line from above to make use of PLINQ (see Listing 2 below):
IEnumerable<int> allNumbers = Enumerable.Range(1, top);
var q = from i in allNumbers.AsParallel() where (i % 2 == 0) select i;
return q.Count();
}
Listing 2: Partial Listing of HelloWorldPlinq.cs
By using the AsParralel() method, we effectively cast q as a different object entirely that can take advantage of threading for its calculations. Now running the application may give you something similar to the following: "Found 500000 even numbers. Elapsed: 14 ms."Got a topic that you'd like to see covered in Edify Friday? Leave a comment below or email me!
Erhan J Kartaltepe, PMP
erhan.kartaltepe-at-utsa.edu










