Subscribe to Blog.Parse(typeof(PsychoCoder), Richard McCutchen);        RSS Feed
-----

C# 4.0 Countdown : New Features - Parallel Programming

Icon Leave Comment
EDIT: I know this was supposed (by my on mouth) been posted last night, but soome things came up and I got behind. I'll try to post one more tonight to be back on track.

In yesterdays post we took at 2 new features coming on April 12, being Optional and Named Parameters. In this entry we'll be looking at what consider to be the biggest new feature Parallel Programming, in short Parallel Programming gives us C# developers the ability to use multi-core processors how they were meant to be used.

Back in the old days most computers in the world had only a single processors, well except those big beast servers, which, of course, were always locked up in rooms with high security (well, usually *nix or Solaris servers) . These servers had multiple processors and it took huge efforts to write softwares and manage them.

Well now in the modern world just about every computer out there, even laptops, have 2, 3 or even 4 processors, but has the technology kept up at a pace to allow us the tools to harness all this computing power, unfortunately no it has not, well not until Microsoft added Parallel Programming to the .Net Framework 4.0 (kudos to you Microsoft) So let's take a small yet revealing look at how parallel programming can change your life, and most importantly change the way you create your applications.

The key to performance improvements is therefore to run a program on multiple processors in parallel and Microsoft has made this happen with the System Concurrency Namespace, which is at the heart of parallel programming. Later we will take a look at a few examples to see how powerful this can be.

If you were to run this following code you would see that the Parallel.For would complete the calculations almost 3 times as fast as with a normal looping structure:

private double WorkerMethod(int calls, int iterationsPerCall, int workPerIteration, bool parallel)
{
    var watch = Stopwatch.StartNew();

    for (int j = 0; j < calls; j++)
    {
        if (parallel)
        {
            Parallel.For(0, iterationsPerCall, k => { WorkerMethodOne(workPerIteration); });
        }
        else
        {
            for (int k = 0; k < iterationsPerCall; k++) 
                WorkerMethodOne(workPerIteration);
        }
    }

    return watch.ElapsedMilliseconds;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private void WorkerMethodOne(int work)
{
    double total = 0;
    for (int j = 0; j < work; j++)
        total += MethodWorkerTwo(j + 2);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private double MethodWorkerTwo(double arg)
{
    if (arg > 10)
        arg -= 10;

    return arg * (arg - 1);
}



So in my opinion Parallel Programming is one of the biggest features Microsoft has given us, and will completely change the world of .Net programming.

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

November 2018

S M T W T F S
    123
45678910
1112131415 16 17
18192021222324
252627282930 

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)