12 June, 2008

Parallel Extensions for .NET

I today had a 10 minute rendez-vous with the Parallel Extensions to .NET 3.5. I was somewhat surprised by how easy it actually was using this stuff. There exists no doubt in my mind that this framework will prove useful in a number of scenarios. You can download the latest bits here.

A normal for-loop looks like this:
private static void RunSingle()
{
    DateTime st = DateTime.Now;
    for(int i = 0; i < maxCount; i++)
    {
        Console.WriteLine(i);
    }

    PrintTime(st, DateTime.Now);
}

and the same logic using Parallel looks like this:
private static void RunParallel()
{
    DateTime st = DateTime.Now;

    var action = newAction<int>(delegate(int i) { Console.WriteLine(i); });
    Parallel.For(0, maxCount, action);
   
    PrintTime(st, DateTime.Now);
}

No comments: