Quantcast
Channel: Wick's Workbench in the Cloud
Viewing all articles
Browse latest Browse all 15

Taking a look at Parallelism

$
0
0

The other day I needed to improve the performance of an application. I spent some time reviewing the code and found a place where the application needed to loop through several hundred to several thousand records. Within the loop the program reached out to another sever to get data, to map the data to a custom object and to apply some business rules. On my test machine, it was taking 3 to 4 seconds for one loop to complete. Just to process 700 records the application needed to run for over 35 minutes. This is not good. The solution that I settled on was take advantage of Parallelism features within .NET Framework (). The code now executes within about 10 minutes on the test machine.

What is Parallelism?

The basic idea behind parallelism is to run multiple tasks at the same time. A slightly larger definition is to partition a task into a small chunk, execute that chunk on its own thread, and to collate the results together.

What Namespace is Parallelism in?

The namespace System.Threading.Tasks provides the types for working with concurrent and asynchronous code.

The class System.Threading.Tasks.Parallel provides the types for working with parallel loops and regions.

When should I consider using Parallelism?

The times that I have found parallelism useful is when I have perform the same operation multiples times and that operations takes more than minute to complete. Another case has been where I have a parent/child task. There have also been cases where long running tasks have benefited from parallelism.

Setting up and running parallel tasks comes at a cost. The code is more complex to write, maintain, and debug. Parallelism also takes up resources on the computer. I need to make sure that there is a large enough benefit (gain in speed) to offset costs (memory and CPU time).

I need to be aware that each task will be on its own thread so I should not expect the results to return to me in the same order as tasks were started.

Show me an example

The key piece of code for starting tasks.

Parallel.ForEach(numList, num =>
    {
        DoSomeWork(num);
    });

The full code example

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

namespace ParallelismExample
{
    class Program
    {
        static void Main(string[] args)
        {

            List<int> numList = Enumerable.Range(0, 100).ToList(); ;
            Parallel.ForEach(numList, num =>
                {
                    DoSomeWork(num);
                });

            Console.WriteLine("Done");
            Console.ReadLine();
        }

        static void DoSomeWork(int taskId)
        {
            Thread t = Thread.CurrentThread;

            Console.WriteLine("Task {0} is running on Thread ID {1}", taskId, t.ManagedThreadId.ToString());

            Thread.Sleep(1000);
        }
    }
}

Where can I find more information? (Resources)

Threading in C# by Joseph Albahari

BlackWasp’s series on Parallelism

System.Threading.Tasks Namespace – MSDN Website

Parallel Class- MSDN Website

Parallel Programming in the .NET Framework – MSDN Website

Parallel.ForEach Method – MSDN Website


Viewing all articles
Browse latest Browse all 15

Latest Images

Trending Articles





Latest Images