Welcome to EMC Consulting Blogs Sign in | Join | Help

Merrick Chaffer's Blog

PLINQ and TPL Parallel extensions library in .NET 4.0

A good starting point for learning about the new parallel library extension in .NET 4.0 is here…

http://blogs.msdn.com/b/pfxteam/archive/2010/04/04/9990342.aspx

also many more resources available from here…

http://msdn.microsoft.com/en-gb/concurrency/default.aspx

Perhaps more useful for quickly assimulating the knowledge, is to watch the videos from Ingo Rammer from the Microsoft Tech Days events here

http://www.microsoft.com/uk/techdays/daydev.aspx

 

image

This is quite interesting. Seems that using the Parallel extensions library doesn't necessarily mean that you will see big performance improvements. I've only found that using my two CPUs in my D830 laptop halves the execution time for a large number of iteration cycles only, mostly due to the cost involved in instantiating the Parallel library classes to start with.

Very interesting example below, if you tweak the value of the iterations constant. (This will work straight out the box in Visual studio 2010, but in VS 2008 you'll need to reference the parallel extensions library)

using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const int iterations = 40000000;
            int value1 = 0;
            int value2 = 0;

            Stopwatch sw = Stopwatch.StartNew();

                Console.WriteLine("Running normally...");

                value1 = GetValue1(iterations);
                value2 = GetValue2(iterations);
                CalculateResult(value1, value2);

            sw.Stop();

            PrintResults(sw);

            sw.Restart();

                Console.WriteLine("Running in parallel...");

                Parallel.Invoke(new ParallelOptions { MaxDegreeOfParallelism = 2 },
                    () => value1 = GetValue1(iterations),
                    () => value2 = GetValue2(iterations));
                CalculateResult(value1, value2);

            sw.Stop();

            PrintResults(sw);

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();

        }

        private static void CalculateResult(int value1, int value2)
        {
            Console.WriteLine("Result = " + (value1 + value2));
        }

        private static void PrintResults(Stopwatch sw)
        {
            Console.WriteLine("Took " + sw.ElapsedMilliseconds + " msec");
        }

        private static int GetValue1(int iterations)
        {
            System.Threading.Thread.SpinWait(iterations);
            return 1;
        }

        private static int GetValue2(int iterations)
        {
            System.Threading.Thread.SpinWait(iterations);
            return 1;
        }
    }
}
Published 14 September 2010 17:55 by merrick.chaffer

Comments

No Comments
Anonymous comments are disabled

This Blog

Syndication

News

Powered by Community Server (Personal Edition), by Telligent Systems