private void StartCalculate(CharFinder finder, Dictionary <int, Stopwatch> stopwatchers, int arrLength)
        {
            finder.ResetValue();

            string[] strToCalculate = new string[arrLength];
            for (int i = 0; i < arrLength; i++)
            {
                strToCalculate[i] = finder.strArr[i];
            }

            Stopwatch   stopwatch = new Stopwatch();
            List <Task> tasks     = new List <Task>();

            foreach (var s in strToCalculate)
            {
                tasks.Add(new Task(() => finder.GetCharCount(s)));
            }

            stopwatch.Start();
            foreach (var t in tasks)
            {
                t.Start();
            }

            Task.WaitAll(tasks.ToArray());
            stopwatch.Stop();
            stopwatchers.Add(arrLength, stopwatch);
        }
Esempio n. 2
0
        static void Main()
        {
            CharFinder        charFinder = new CharFinder();
            StopwatchAnalyzer analyzer   = new StopwatchAnalyzer();

            Console.WriteLine("Thread class\n");
            IMultithreadable multithreadable = new MyFactoryThread();

            analyzer.Analyze(multithreadable.Execute(charFinder));

            Console.WriteLine("\nTasks\n");
            multithreadable = new MyFactoryTask();
            analyzer.Analyze(multithreadable.Execute(charFinder));

            Console.WriteLine("\nParallel class\n");
            multithreadable = new MyParallel();
            analyzer.Analyze(multithreadable.Execute(charFinder));

            Console.WriteLine("\nasync / await\n");
            MyAsync myAsync = new MyAsync();

            analyzer.Analyze(myAsync.Execute(charFinder).Result);

            Console.ReadLine();
        }
Esempio n. 3
0
        public async Task <Dictionary <int, Stopwatch> > Execute(CharFinder finder)
        {
            Dictionary <int, Stopwatch> stopwatchers = new Dictionary <int, Stopwatch>();

            await StartCalculate(finder, stopwatchers, 100);
            await StartCalculate(finder, stopwatchers, 500);
            await StartCalculate(finder, stopwatchers, 5000);
            await StartCalculate(finder, stopwatchers, 50000);
            await StartCalculate(finder, stopwatchers, 500000);

            return(stopwatchers);
        }
Esempio n. 4
0
        public Dictionary <int, Stopwatch> Execute(CharFinder finder)
        {
            Dictionary <int, Stopwatch> stopWatchers = new Dictionary <int, Stopwatch>();

            StartCalculate(finder, stopWatchers, 100);
            StartCalculate(finder, stopWatchers, 500);
            StartCalculate(finder, stopWatchers, 5000);
            StartCalculate(finder, stopWatchers, 50000);
            StartCalculate(finder, stopWatchers, 500000);

            return(stopWatchers);
        }
Esempio n. 5
0
        private async Task StartCalculate(CharFinder finder, Dictionary <int, Stopwatch> stopwatchers, int arrLength)
        {
            finder.ResetValue();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < arrLength; i++)
            {
                await finder.GetCharCountAsync(finder.strArr[i]);
            }
            stopwatch.Stop();
            stopwatchers.Add(arrLength, stopwatch);
        }
        private void StartCalculate(CharFinder finder, Dictionary <int, Stopwatch> stopwatchers, int arrLength)
        {
            Stopwatch stopwatch = new Stopwatch();

            finder.ResetValue();

            stopwatch.Start();
            for (int j = 0; j < arrLength; j++)
            {
                new Thread(finder.GetCharCount).Start(finder.strArr[j]);
            }
            stopwatch.Stop();
            stopwatchers.Add(arrLength, stopwatch);
        }
Esempio n. 7
0
        private void StartCalculate(CharFinder finder, Dictionary <int, Stopwatch> stopWatchers, int arrLength)
        {
            finder.ResetValue();

            string[] strToCalculate = new string[arrLength];
            for (int i = 0; i < arrLength; i++)
            {
                strToCalculate[i] = finder.strArr[i];
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            Parallel.ForEach <object>(strToCalculate,
                                      finder.GetCharCount);
            stopwatch.Stop();
            stopWatchers.Add(arrLength, stopwatch);
        }