Hi, in this blog we are going to discuss a very important feature of C# that is data parallelism. Data parallelism means the concurrent execution process or processes.
Background:
Data Parallelism comes in a scenario when we have a situation where a single process is required to be executed multiple times. Let’s take an example to understand this.
Suppose we need to send data from a web application to WEB API (web service), we will be sending data synchronously using batch processing. Batch processing will require loops (for, while or foreach). This will take some time to complete this process. We can shorten this time if we go with parallel processing rather than going to synchronous call.
Details
Data parallelism is used in cases where same operation is performed parallely in same period of time, on any data source in the form of an array or collection. This data source is portioned and distributed among the threads. Each thread has a different segment of data to perform the operation.
This is achieved by using Task parallel library (Tpl) in c#. TPL implements data parallelism by using System.Threading.Tasks.Parallel class. This class provides method-based parallel implementations of for and for each loops.
Now Let’s walk through the code of Synchronous Process and Parallel process.
Synchronous Process (Using Loop)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace TestingApp { class DataParallelism { static void Main(string[] args) { SynchronousProcess(); } private static void PostData() { // Code to send data to web API ( web service ). Thread.Sleep(60000); } private static void SynchronousProcess() { Console.WriteLine("Syncronus Process Started"); DateTime startTime = DateTime.Now; List<int> batchList = new List<int> { 1, 2, 3, 4, 5 }; Console.WriteLine("Start Time:" + startTime); foreach (int item in batchList) { Console.WriteLine("Begin task..." + item); PostData(); Console.WriteLine("End task..." + item); } DateTime endTime = DateTime.Now; Console.WriteLine("End Time:" + endTime); TimeSpan timeExecution = endTime - startTime; Console.WriteLine("Total time execution :" + timeExecution); Console.WriteLine("Syncronus Process End"); Console.ReadLine(); } } }
Output:
Syncronus Process Started
Start Time:12-08-17 12.05.24 AM
Begin task...1
End task...1
Begin task...2
End task...2
Begin task...3
End task...3
Begin task...4
End task...4
Begin task...5
End task...5
End Time:12-08-17 12.10.24 AM
Total time execution :00:05:00.0612860
Syncronus Process End
Synchronous Process in Data Parallelism in C# |
Now lets see how parallel processing works
Parallel Process
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ParallelInvokeMethods { class DataParallelism { static void Main(string[] args) { ParallelProcess(); } private static void PostData() { // Code to send data to web API ( web service ). Thread.Sleep(60000); } private static void ParallelProcess() { Console.WriteLine("Parallel Process Started"); DateTime startTime = DateTime.Now; List<int> batchList = new List<int> { 1, 2, 3, 4, 5 }; Console.WriteLine("Start Time:" + startTime); Parallel.ForEach(batchList, item => { Console.WriteLine("Begin task..." + item); PostData(); Console.WriteLine("End task..." + item); }); DateTime endTime = DateTime.Now; Console.WriteLine("End Time:" + endTime); TimeSpan timeExecution = endTime - startTime; Console.WriteLine("Total time execution :" + timeExecution); Console.WriteLine("Parallel Process End"); Console.ReadLine(); } } }
Output:
Parallel Process Started
Start Time:12-08-17 12.16.40 AM
Begin task...1
Begin task...2
Begin task...4
Begin task...3
Begin task...5
End task...1
End task...2
End task...3
End task...4
End task...5
End Time:12-08-17 12.17.40 AM
Total time execution :00:01:00.2737825
Parallel Process End
Parallel Process in Data Parallelism in C# |
Summary :
So, Guys this was all about the Parallel Process in Data Parallelism in C#.
I Hope this post will be helpful to understand the Parallel task execution approach.
Please share this post with your friends and colleagues to help them clearing the concept of Data Parallelism in C#.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.