0

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;


namespace ConsoleApplication2
{
    public class c_Thread
    {
        public bool ThreadOngoing;
        public c_Thread()
        {
            ThreadOngoing = false;
        }
        public void CallToChildThread1(string key, ref List<int> nums, ref ConcurrentDictionary<string, List<int>> container)
        {
            container.TryAdd(key, nums);
        }

        public void CallToChildThread2(string key, ref List<int> nums, ref Dictionary<string, List<int>> container)
        {
            container.Add(key, nums);
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            RunProgram();
            Console.ReadKey();
        }
        static void RunProgram()
        {
            System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
            Console.WriteLine("Normal");
            Program1();
            sw.Stop();
            Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);
            sw.Reset();
            sw.Start();
            Console.WriteLine("Parallel");
            Program2();
            sw.Stop();
            Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);
        }

        static void Program1()
        {

            c_Thread cc = new c_Thread();
            Dictionary<string, List<int>> container1 = new Dictionary<string, List<int>>();
            List<int> aList = new List<int>();
            List<int> bList = new List<int>();
            string printLine;
            aList.Add(2);
            aList.Add(4);
            aList.Add(2);
            aList.Add(2);
            aList.Add(4);
            aList.Add(2);
            aList.Add(2);
            aList.Add(4);
            aList.Add(2);
            aList.Add(2);
            aList.Add(4);
            aList.Add(2);

            bList.Add(1);
            bList.Add(3);
            bList.Add(1);
            bList.Add(1);
            bList.Add(3);
            bList.Add(1);
            bList.Add(1);
            bList.Add(3);
            bList.Add(1);
            bList.Add(1);
            bList.Add(3);
            bList.Add(1);
            cc.CallToChildThread2("param1", ref aList, ref container1);
            cc.CallToChildThread2("param2", ref bList, ref container1);
            foreach (string key in container1.Keys)
            {
                printLine = key + ": ";
                foreach (int val in container1[key])
                    printLine += val + " ";
                Console.WriteLine(printLine);

            }
        }

        static void Program2()
        {
            c_Thread cc = new c_Thread();
            ConcurrentDictionary<string, List<int>> container1 = new ConcurrentDictionary<string, List<int>>();
            List<int> aList = new List<int>();
            List<int> bList = new List<int>();
            string printLine;
            aList.Add(2);
            aList.Add(4);
            aList.Add(2);
            aList.Add(2);
            aList.Add(4);
            aList.Add(2);
            aList.Add(2);
            aList.Add(4);
            aList.Add(2);
            aList.Add(2);
            aList.Add(4);
            aList.Add(2);

            bList.Add(1);
            bList.Add(3);
            bList.Add(1);
            bList.Add(1);
            bList.Add(3);
            bList.Add(1);
            bList.Add(1);
            bList.Add(3);
            bList.Add(1);
            bList.Add(1);
            bList.Add(3);
            bList.Add(1);
            Thread myNewThread1 = new Thread(() => cc.CallToChildThread1("param1", ref aList, ref container1));
            myNewThread1.Start();
            Thread myNewThread2 = new Thread(() => cc.CallToChildThread1("param2", ref bList, ref container1));
            myNewThread2.Start();
            myNewThread1.Join();
            myNewThread2.Join();
            foreach (string key in container1.Keys)
            {
                printLine = key + ": ";
                foreach (int val in container1[key])
                    printLine += val + " ";
                Console.WriteLine(printLine);

            }
        }
    }
}

The two methods Program1() and Program2() do the same thing. Program1() calls CallToChildThread2() to input a key value pair into the dictionary however, Program2() calls CallToChildThread1() to input key value pairs into a concurrentdictionary parallely.

The output is the following:

Output

Adhil
  • 1,678
  • 3
  • 20
  • 31
  • 2
    _"The two methods Program1() and Program2() do the same thing"_ -- I disagree. You even acknowledge they do something different. More to the point, the difference between them is more than just parallel and not parallel. You are comparing apples to oranges here. – Peter Duniho Feb 22 '17 at 03:45
  • by doing the same thing I mean that they both input key value pairs into a dictionary, I am trying to determine which method is faster – Adhil Feb 22 '17 at 03:55
  • You already have determined which is faster. Your question seems to be concerned with you not being happy with that determination. But you haven't explained why, especially given that the two variations accomplish their goals in very different ways (again, not just one being parallel and the other not). – Peter Duniho Feb 22 '17 at 03:57
  • There probably some serious question behind this post - so far it looks like you are asking whether doing whole bunch of work on top of adding item to dictionary will have measurable impact... At least to me it sounds quite obvious that doing extra work will slow things down... – Alexei Levenkov Feb 22 '17 at 03:58
  • I was under the impression that doing things parallaly would be faster. – Adhil Feb 22 '17 at 04:02
  • You should learn to use a profiler. There's one in Visual Studio. You will likely find that you spend very little time actually in the calls to `CallToChildThread1()` and `CallToChildThread2()`. Threads are very costly to start, especially from scratch. Most of the time in your "parallel" example is likely due to that. You can improve things by using the thread pool, e.g. `Task.Run()`. But no matter what you do, if all you're doing is adding a _single_ key/value pair to a dictionary with each operation, there's no way parallelizing that is going to help. There's just too much overhead. – Peter Duniho Feb 22 '17 at 04:06
  • For parallelism to help, you need to be able to encapsulate the work into units large enough that the act of pushing the work to a worker thread is miniscule compared to the cost of the work itself. And of course, for that to help, you need enough work to be able to create two or more of these large work units. – Peter Duniho Feb 22 '17 at 04:07

0 Answers0