Data Parallelism in C#

Introduction
In this blog, will have walk through about Data Parallelism, where we could execute process concurrently.

Background
Some time, we come to scenario where we need to execute same process multiple times and that would take a while to complete the operation.

Example: Suppose we need to send data from web application to WEB API ( web service ) , So we will be sending data from web application to web service using batch processing. So definitely for this implementation will go with loop (for, while, foreach etc.) and that would be synchronous call, so would take a while to complete the process.

Rather going with synchronous call if we go with parallel processing then execution would be quicker compare to loop.

Details
Data parallelism refers to scenarios in which the same operation is performed concurrently (that is, in parallel) on elements in a source collection or array. In data parallel operations, the source collection is partitioned so that multiple threads can operate on different segments concurrently. 

The Task Parallel Library (TPL) supports data parallelism through the System.Threading.Tasks.Parallel class. This class provides method-based parallel implementations of for and for each loops.

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 ParallelInvokeMethods
{
    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");
        }
    }
}


Result

Synchronous Process Started
    Start Time:5/30/2015 12:49:29 PM
    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:5/30/2015 12:54:29 PM
    Total time execution :00:05:00.0333292
Synchronous Process End


Here we can see Synchronous process execution took 5 minutes to complete the operation.

Whereas Parallel process is very fast.

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");
        }
    }
}


Result

Parallel Process Started
    Start Time:5/30/2015 12:57:29 PM
    Begin task...1
    Begin task...2
    Begin task...3
    Begin task...4
    Begin task...5
    End task...1
    End task...2
    End task...4
    End task...3
    End task...5
    End Time:5/30/2015 12:58:30 PM
    Total time execution :00:01:00.5756072
Parallel Process End

This code took “1” minutes to complete the operation using Parallel task execution. Quite fast.

Summary

Hope this post will be helpful to understand the Parallel task execution approach.

Write a comment
Cancel Reply
  • Lavon April 29, 2016, 7:28 pm
    I simply want to mention I am beginner to blogging and sieitbuild-ng and certainly liked your blog site. Almost certainly i want to bookmark your website . You amazingly come with exceptional posts. Many thanks for sharing with us your blog site.
    reply
  • Arco September 21, 2015, 5:10 am
    Article is really fruitful for me, keep up posting such articles or reviews.
    reply
  • Jeema June 15, 2015, 5:19 pm
    Great blog and I look forward to seeing new more!!
    reply
  • Aarti June 15, 2015, 11:51 am
    Keep posting such great blogs
    reply