Esempio n. 1
0
        static void Tests(string path, int testsDozensCount)
        {
            Console.WriteLine($"Test started.\nFile: {path}\nCount of tests: {testsDozensCount * 10}\nStatus:");
            string text = "";

            if (!Read(path, ref text))
            {
                return;
            }

            TimeSpan oneThreadTime    = TimeSpan.Zero;
            TimeSpan minOneThreadTime = TimeSpan.MaxValue;
            int      maxThreadsCount  = 20;

            TimeSpan[,] multyThreadTime    = new TimeSpan[2, maxThreadsCount];
            TimeSpan[,] minMultyThreadTime = new TimeSpan[2, maxThreadsCount];
            for (int i = 0; i < maxThreadsCount; i++)
            {
                multyThreadTime[0, i]    = TimeSpan.Zero;
                minMultyThreadTime[0, i] = TimeSpan.MaxValue;
                multyThreadTime[1, i]    = TimeSpan.Zero;
                minMultyThreadTime[1, i] = TimeSpan.MaxValue;
            }
            MethodInfo mi = typeof(Counter).GetMethod("Parse", BindingFlags.Static | BindingFlags.NonPublic);

            Stopwatch stopwatch = new Stopwatch();

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine($"{i * 10}%");
                for (int j = 0; j < testsDozensCount; j++)
                {
                    stopwatch.Start();
                    mi.Invoke(null, new object[] { text });
                    stopwatch.Stop();
                    oneThreadTime += stopwatch.Elapsed;
                    if (minOneThreadTime > stopwatch.Elapsed)
                    {
                        minOneThreadTime = stopwatch.Elapsed;
                    }
                    stopwatch.Reset();

                    for (int k = 0; k < maxThreadsCount; k++)
                    {
                        stopwatch.Start();
                        Counter.AsyncParse(text, k + 1);
                        stopwatch.Stop();
                        multyThreadTime[0, k] += stopwatch.Elapsed;
                        if (minMultyThreadTime[0, k] > stopwatch.Elapsed)
                        {
                            minMultyThreadTime[0, k] = stopwatch.Elapsed;
                        }
                        stopwatch.Reset();

                        stopwatch.Start();
                        Counter.ConcurentParse(text, k + 1);
                        stopwatch.Stop();
                        multyThreadTime[1, k] += stopwatch.Elapsed;
                        if (minMultyThreadTime[1, k] > stopwatch.Elapsed)
                        {
                            minMultyThreadTime[1, k] = stopwatch.Elapsed;
                        }
                        stopwatch.Reset();
                    }
                }
            }
            Console.WriteLine("100%");

            Console.WriteLine($"Обработка стандартного метода заняла {oneThreadTime.TotalMilliseconds / testsDozensCount / 10} миллисекунд.");
            Console.WriteLine($"Threads| Time | Min  | Time | Min ");
            for (int i = 0; i < maxThreadsCount; i++)
            {
                Console.WriteLine($"{i+1,7}|{multyThreadTime[0,i].TotalMilliseconds / testsDozensCount / 10,6:F2}|{minMultyThreadTime[0,i].TotalMilliseconds,6:F2}|{multyThreadTime[1, i].TotalMilliseconds / testsDozensCount / 10,6:F2}|{minMultyThreadTime[1, i].TotalMilliseconds,6:F2}");
            }
        }