Click here to Skip to main content
15,891,184 members
Articles / General Programming / Threads

Task Parallel Library: 4 of n

Rate me:
Please Sign up or sign in to vote.
4.93/5 (59 votes)
10 May 2011CPOL15 min read 121.4K   1.7K   126  
A look into using the Task Parallel Library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ParallelLINQ.Common;
using System.Threading;
using System.Diagnostics;


namespace SimpleParrallelLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            ManualResetEventSlim mre = new ManualResetEventSlim();

            //***********************************************************************************
            //
            //   SCENARIO 1 : Sequential LINQ
            //
            //***********************************************************************************
            Stopwatch watch = new Stopwatch();
            watch.Start();
            IEnumerable<double> results = StaticData.DummyRandomIntValues.Value
                                        .Select(x => Math.Pow(x, 2));
            foreach (int item in results)
            {
                Console.WriteLine("Result is {0}", item);
            }
            watch.Stop();
            Console.WriteLine("time ellapsed for Sequential version {0}ms", watch.ElapsedMilliseconds);
            mre.Set();


            //***********************************************************************************
            //
            //   SCENARIO 2 : Possibly Parallel LINQ, possibly extra overhead is inccurred as TPL
            //                must analyze the query to decide if it would better run Sequentially 
            //                or using Task(s)
            //
            //***********************************************************************************
            mre.Wait();
            mre.Reset();
            Stopwatch watch2 = new Stopwatch();
            watch2.Start();
            var results2 = StaticData.DummyRandomIntValues.Value.AsParallel()
                .Select(x => Math.Pow(x, 2));

            foreach (int item in results2)
            {
                Console.WriteLine("Result is {0}", item);
            }
            watch2.Stop();
            Console.WriteLine("time ellapsed for Possibly Parrallel LINQ version {0}ms", watch2.ElapsedMilliseconds);
            mre.Set();



            //***********************************************************************************
            //
            //   SCENARIO 3 : Parallel LINQ
            //
            //***********************************************************************************
            mre.Wait();
            mre.Reset();
            Stopwatch watch3 = new Stopwatch();
            watch3.Start();
            var results3 = StaticData.DummyRandomIntValues.Value
                .AsParallel()
                .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                .Select(x => Math.Pow(x, 2));

            foreach (int item in results3)
            {
                Console.WriteLine("Result is {0}", item);
            }
            watch3.Stop();
            Console.WriteLine("time ellapsed for Parrallel LINQ version {0}ms", watch3.ElapsedMilliseconds);


            Console.ReadLine();

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions