Task parallel library

ron naber
.NET voyage
Published in
3 min readOct 26, 2014

--

tutorial and small demo with code

Why to use task parallel library — To improve performance of application by dividing the work between processors
equally.As hardware is improving ie more number of processor in servers , use of task will be very beneficial.

without task parallel library ( a feature of 4.5 Net framework ) , multiple threads are time sliced (context switched)

example : suppose there are two threads running parallely , then thread 1 will run for lets say 20 seconds and then it
will give chance for thread 2 to run for 20 seconds . This is cycle but it can cut down performance of the application.

To check the performance of your application and analyse the distribution of processor when application is running ,
we will use tool called perfmon (performance monitor).It is very useful tool and easy to find too.Even if
you do not have this app , we could use stopwatch class of c# to find the timings.

Without Task parallel library

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;

namespace _26102014tasklibrary
{ class Program { static void Main(string[] args) { Thread O1 = new Thread(RunMillionIterations); O1.Start(); Console.ReadLine(); }

public static void RunMillionIterations() { Stopwatch stp = new Stopwatch(); stp.Start(); for (int i = 0; i <= 100000; i++) { Console.WriteLine(i); } stp.Stop(); TimeSpan ts = stp.Elapsed;

string elapsedTime = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine(“RunTime “ + elapsedTime); }

}
}

With Task parallel library

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;

namespace _26102014tasklibrary
{ class Program { static void Main(string[] args) { Parallel.For(0, 1, x => RunMillionIterations()); //we use parallel class to say that run this function in multiple processors Console.ReadLine(); }

public static void RunMillionIterations() { Stopwatch stp = new Stopwatch(); stp.Start(); for (int i = 0; i <= 100000; i++) { Console.WriteLine(i); } stp.Stop(); TimeSpan ts = stp.Elapsed;

string elapsedTime = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine(“RunTime “ + elapsedTime); }

}
}

Time taken without Task parallel library

8 sec 36 milliseconds

Time take with Task parllel library

8 sec 29 milliseconds

There are many more things to explore in task parallel library and its fun.

Now lets talk about Parallel method called Parallel.Invoke()

In simple words , Parallel.Invoke() methods accepts two types of parameters
They are 1) array of functions 2) two functions.

so what happens when you pass lets say two methods in Parallel.Invoke() method
The answer is the both the functions work simultaneosly in different cores saving time.
If you have two methods in your program which can run in parallel ,
lets say console.writeline() and a user created serialisetoxml() which can run parallel to each other
because neither affects each other , then why keep them in tetris mode (run console.writeline() first and then
go for serialisetoxml()) and waste time. Just run them in parallel.
Here in my example i have two methods which are not related to each other, hence i can introduce asynchronous
programming.Just try this program and see the output,
What you will find is that the time taken for both of them to complete
if they run individually one after the other is much more than if they run with parallelly.
Use can use the timer in the program to find time difference.
In my system it takes 12 seconds parallely and 28 seconds if in tetris mode.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;

namespace _26102014tasklibrary
{ class Program { static void Main(string[] args) { Parallel.Invoke(() => SaySomething(), () => RunMillionIterations()); //Thread O1 = new Thread(RunMillionIterations); //O1.Start(); //Parallel.For(0, 1, x => RunMillionIterations()); Console.ReadLine(); } public static void SaySomething() { for (int i = 0; i <= 100000; i++) { Console.WriteLine(“this is how we roll”); } } public static void RunMillionIterations() { Stopwatch stp = new Stopwatch(); stp.Start(); for (int i = 0; i <= 100000; i++) { Console.WriteLine(i); } stp.Stop(); TimeSpan ts = stp.Elapsed;

string elapsedTime = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine(“RunTime “ + elapsedTime); }

}
}

--

--

ron naber
.NET voyage

writing sets you free , lets you explore and enjoy life more, so read n write