static void Main(string[] args)
        {
            var watch = new Stopwatch();

            int[] valuesToSearch = { 557246, 778451, 11111111 };

            for (int j = 0; j < 3; j++)
            {
                int starting = 0;
                int ending   = 200000;
                sharedVariableOfThreadCounter = 5;
                Thread[] threadArray = new Thread[5];
                watch.Restart();

                for (int i = 0; i < 5; i++)
                {
                    SearchArrayCallback callback = new SearchArrayCallback(searchArrayCallbackFunction);
                    HelperClass         withFiveThreadOneThreadAtaTime = new HelperClass(valuesToSearch[j], i, starting, ending, callback);
                    threadArray[i] = new Thread(new ThreadStart(withFiveThreadOneThreadAtaTime.searchArray));
                    threadArray[i].Start();
                    starting += 200000;
                    ending   += 200000;
                }
                for (int i = 0; i < 5; i++)
                {
                    threadArray[i].Join();
                }
                watch.Stop();
                Console.WriteLine($"Execution Time : {watch.ElapsedMilliseconds} ms");
            }
            Console.ReadKey();
        }
Beispiel #2
0
        /*
         * try
         *  {
         *      using (StreamWriter writer = new StreamWriter(textFile))
         *          {
         *              for (int i = 1; i <= 100000; i++)
         *              {
         *                  Random r = new Random();
         *                  writer.WriteLine(r.Next(10,1000000));
         *              }
         *          }
         *  }
         * catch (Exception exp)
         *  {
         *      Console.Write(exp.Message);
         *  }*/
        #endregion
        static void Main(string[] args)
        {
            var watch       = new Stopwatch();
            var threadWatch = new Stopwatch();

            int[] valuesToSearch = { 557246, 778451, 11111111 };

            PartA  withoutThread = new PartA();
            string textFile      = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.FullName, "Values.txt");

            string[] lines = File.ReadAllLines(textFile);


            Console.WriteLine("\t-----------Part A Searching without Threads -----------\n");
            watch.Start();
            for (int j = 0; j < 3; j++)
            {
                watch.Restart();
                var x = withoutThread.searchArray(valuesToSearch[j], lines);
                watch.Stop();
                Console.WriteLine($"\n{x.Item3} Array Contains Value {x.Item1}  at Index: {x.Item2}");
                Console.WriteLine($"Execution Time : {watch.ElapsedMilliseconds} ms");
            }

            Console.WriteLine("\n\nPress Enter to Continue");
            Console.ReadKey();

            Console.WriteLine("\n\n\t-----------Part B Searching with 5 Threads -----------\n");
            for (int j = 0; j < 3; j++)
            {
                int      starting    = 0;
                int      ending      = 200000;
                Thread[] threadArray = new Thread[5];
                sharedVariableOfThreadCounter = 5;
                watch.Restart();
                for (int i = 0; i < 5; i++)
                {
                    // Callback used inorder to receive data back from the thread
                    SearchArrayCallback searchArrayCallback = new SearchArrayCallback(searchArrayCallbackFunction);
                    threadWatch.Restart();

                    //Note. Passing the  Stopwatch variable to the helper class inorder to get the execution time of Individual thread
                    // This stopwatch would be stop and the execution time of thread would be returned via callback function and printed there
                    PartB withFiveThread = new PartB(valuesToSearch[j], i, starting, ending, searchArrayCallback, threadWatch);
                    threadArray[i] = new Thread(new ThreadStart(withFiveThread.searchArray));
                    threadArray[i].Start();
                    starting += 200000;
                    ending   += 200000;
                }
                for (int i = 0; i < 5; i++)
                {
                    threadArray[i].Join();
                }
                watch.Stop();
                Console.WriteLine($"Total Execution Time of 5 Threads : {watch.ElapsedMilliseconds} ms");
            }
            Console.ReadKey();
        }
        public HelperClass(int value, int threadIndex, int startIndex, int lastIndex, SearchArrayCallback s)
        {
            string textFile = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.FullName, "Values.txt");

            _value               = value;
            _threadIndex         = threadIndex;
            _startIndex          = startIndex;
            _lastIndex           = lastIndex;
            _searchArrayCallback = s;
            lines = File.ReadAllLines(textFile);
        }
Beispiel #4
0
        public PartB(int value, int threadIndex, int startIndex, int lastIndex, SearchArrayCallback s, Stopwatch stopwatch)
        {
            string textFile = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.FullName, "Values.txt");

            _value               = value;
            _threadIndex         = threadIndex;
            _startIndex          = startIndex;
            _lastIndex           = lastIndex;
            _searchArrayCallback = s;
            lines = File.ReadAllLines(textFile);
            currentThreadExecutionTime = stopwatch;
            currentThreadExecutionTime.Start();
        }