• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Saturday, August 12, 2017

Data Parallelism in C#

 Abhishek Tomer     August 12, 2017     .Net, C#     No comments   

Introduction:
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#
Synchronous Process in Data Parallelism in C#
Here we can see Synchronous process execution took 5 minutes to complete the operation.

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#
Parallel Process in Data Parallelism in C#
This code took “1” minute to complete the operation using Parallel task execution. Quite fast!! 

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 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • OOPS – Difference Between Class and Struct
    Hi friends! Today we are going to discuss a very quite interesting and important topic of Object Oriented Programming(OOPs) i.e. “Classes an...
  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...
  • Usability of SecureString object in C#
    Introduction Hi friends! In this blog we will be discussing a very interesting as well as useful topic in C# and that is Securestring objec...
  • Application Life cycle of Asp.Net MVC
    Today i am going to tell you the Application life cycle of an Asp.Net MVC Application, this post is important for those who are new to Asp.N...
  • C# Programming Interview Questions
    Today i am going to tell you some of the basic programming questions that are frequently asked in interview for fresher and experienced as w...
  • Cascading referential integrity constraint in Sql Server
    Hi friends! In our previous sessions we have seen how to create tables and enforce primary and foreign key constraints between them . And in...
  • Model Binding in ASP.NET Core
    In this article, I am going to discuss Model Binding in ASP.NET Core Application. Please read our previous article where we discussed how ...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape