Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Task Parallel Library: 4 of n

, 10 May 2011
A look into using the Task Parallel Library.
Tasks4.zip
Tasks4
Cancellation
bin
Debug
Cancellation.exe
Cancellation.pdb
Cancellation.vshost.exe
Cancellation.vshost.exe.manifest
ParallelLINQ.Common.dll
ParallelLINQ.Common.pdb
obj
x86
Debug
Cancellation.exe
Cancellation.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
CatchingExceptions
bin
Debug
CatchingExceptions.exe
CatchingExceptions.pdb
CatchingExceptions.vshost.exe
CatchingExceptions.vshost.exe.manifest
ParallelLINQ.Common.dll
ParallelLINQ.Common.pdb
obj
x86
Debug
CatchingExceptions.exe
CatchingExceptions.pdb
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
TempPE
Properties
CustomAggregation
bin
Debug
CustomAggregation.exe
CustomAggregation.pdb
CustomAggregation.vshost.exe
CustomAggregation.vshost.exe.manifest
ParallelLINQ.Common.dll
ParallelLINQ.Common.pdb
obj
x86
Debug
CustomAggregation.exe
CustomAggregation.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
CustomPartitioning
bin
Debug
CustomPartitioning.exe
CustomPartitioning.pdb
CustomPartitioning.vshost.exe
CustomPartitioning.vshost.exe.manifest
ParallelLINQ.Common.dll
ParallelLINQ.Common.pdb
obj
x86
Debug
CustomPartitioning.exe
CustomPartitioning.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
ParallelLINQ.Common
bin
Debug
ParallelLINQ.Common.dll
ParallelLINQ.Common.pdb
Release
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ParallelLINQ.Common.dll
ParallelLINQ.Common.pdb
TempPE
Properties
ParallelRange
bin
Debug
ParallelRange.exe
ParallelRange.pdb
ParallelRange.vshost.exe
ParallelRange.vshost.exe.manifest
obj
x86
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ParallelRange.exe
ParallelRange.pdb
TempPE
Properties
SimpleOrdering
bin
Debug
ParallelLINQ.Common.dll
ParallelLINQ.Common.pdb
SimpleOrdering.exe
SimpleOrdering.pdb
SimpleOrdering.vshost.exe
SimpleOrdering.vshost.exe.manifest
obj
x86
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
SimpleOrdering.exe
SimpleOrdering.pdb
TempPE
Properties
Service References
SimpleParrallelLinq
bin
Debug
ParallelLINQ.Common.dll
ParallelLINQ.Common.pdb
SimpleParrallelLinq.exe
SimpleParrallelLinq.pdb
SimpleParrallelLinq.vshost.exe
SimpleParrallelLinq.vshost.exe.manifest
obj
x86
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
SimpleParrallelLinq.exe
SimpleParrallelLinq.pdb
TempPE
Properties
Tasks4.suo
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)

Share

About the Author

Sacha Barber
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 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

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 10 May 2011
Article Copyright 2011 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid