Bad Parallel Computing in .NET 4 | Learning Tree Blog

evden eve nakliyat malatya oto kiralama parça eşya taşıma istanbul şehirler arası nakliyat fabrika taşımacılığı malatya oto kiralama istanbul evden eve nakliyat kadıköy evden eve nakliyat istanbul ofis taşıma evden eve nakliyat evden eve nakliyat büyü aşk büyüsü ayırma büyüsü medyum medyum şikayetleri medyum yorumları büyü aşk büyüsü bağlama büyüsü dua aşk duası aşk büyüsü büyü aşk büyüsü bağlama büyüsü medyum dolunay medyum aşk büyüsü medyum medyum şikayetleri medyum yorumları medyum büyü dua galvanizli sac hrp sac tekne turu yat kiralama gulet kiralama mekjoy seo instagram takipçi satın al sex shop kartal sex shop üsküdar sex shop istanbul eşya depolama istanbul eşya depolama shell aspx shell alfa shell şehirler arası nakliyat istanbul şehirler arası nakliyat

Parallel Computing in .NET 4

Almost all computers today have multiple CPU cores. .NET 4 allows you to easily take advantage of all that computing power, very easily.  One way is to use the Parallel class, which is included in the System.Threading.Tasks namespace.

Let’s take a look at an example.  In the method below, all that happens is a number gets squared and is sent to the screen.  To simulate a long running task, each time the method is called the thread goes to sleep for random amounts of time between 100 and 1000 milliseconds.

private void Square(int i)

{

    Random rnd = new Random();

    int wait = rnd.Next(1, 10) * 100;

    Thread.Sleep(wait);

    Console.WriteLine(i * i);

}

Inside the same class, is a method called Execute() that uses a for-each loop to square for the numbers 1 through 10.  It is shown below.

public void Execute()

{

    var integers = Enumerable.Range(1, 10);

    foreach (var i in integers)

    { 

        Square(i);

    }

}

In the following code Execute() is called, and each squared number is put on the screen.  Then the time it takes to run through the loop is calculated and displayed as well.

DateTime start = DateTime.Now;

LongCommand cmd = new LongCommand();

cmd.Execute();

TimeSpan elapsed =  DateTime.Now.Subtract(start);

Console.WriteLine(

    String.Format("It took {0} milliseconds.",

    elapsed.TotalMilliseconds));

The output is shown below.  It varies each time, but takes around four seconds for the code to complete.  Notice, as you would expect, the squared numbers come out in sequence.


Loop using 1 CPU core

Below is a variation of the prior Execute() method, ExecuteInParallel().  However, instead of using a for-each loop, it uses the Parallel class ForEach() method.  This method will take advantage of the CPU cores it has available to it.  The collection of numbers is passed as an argument to ForEach(), along with a lambda expression that calls Square() on each number in the collection. (If you don’t understand lambda expressions, see my previous post:Understanding Lambda Expressions in C#).

public void ExecuteInParallel()

{

    var integers = Enumerable.Range(1, 10);

    Parallel.ForEach(integers, i => Square(i))

}

Now, if you call ExecuteInParallel(), rather than Execute() the output is shown below.  This was run on a machine with a dual-core CPU.  As you would expect it takes about half as long to run.  Also notice, the numbers don’t come out in order.  This proves that both cores are being utilized.


Loop using 2 CPU cores

You can download the code from my web site at: http://www.drehnstrom.com/downloads/ParallelExample.zip.

If you’re not a .NET Programmer, but you would like to learn more about it, attend Learning Tree course 502: Programming with .NET: A Comprehensive Hands-On Introduction.

If you are a .NET programmer, and you would like to learn more about writing .NET applications for Windows Azure, come to Learning Tree course 2602: Windows Azure Platform Introduction: Programming Cloud-Based Applications.

Doug Rehnstrom

Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.

maltepe escort kartal escort ataşehir escort anadolu yakası escort pendik escort maltepe escort kadıköy escort escort bayan göztepe escort kartal escort kurtköy escort