BelhardTraining.QuadraticEquationBenchmark.Program.BenchmarkWithThreads C# (CSharp) Метод

BenchmarkWithThreads() приватный статический Метод

Замеряет и выводит на экран время потраченное на решение уравнений с использованием потоков
private static BenchmarkWithThreads ( int totalEquationCount, int threadCount, ThreadPriority priority = ThreadPriority.Normal ) : void
totalEquationCount int Общее количество уравнений которые необходимо решить. Делится между потоками
threadCount int Кол-во потоков которые следует использовать для параллельного решения уравнений
priority ThreadPriority Приоритет потока
Результат void
        private static void BenchmarkWithThreads(int totalEquationCount, int threadCount, ThreadPriority priority = ThreadPriority.Normal)
        {
            if (totalEquationCount % threadCount != 0) throw new Exception();

            Console.Write("Время на решение  с потоками: ");
            int itemsPerThread = totalEquationCount / threadCount; // Кол-во уравнений для каждого потока

            Stopwatch watch = Stopwatch.StartNew();
            Thread[] solveThreads = new Thread[threadCount];
            for (int i = 0, offset = 0; i < solveThreads.Length; i++, offset += itemsPerThread)
            {
                solveThreads[i] = new Thread(SolveThread)
                {
                    Name = String.Format("Решатель уравнений #{0}", i + 1),
                    Priority = priority
                };
                solveThreads[i].Start(itemsPerThread);
            }
            foreach (Thread solveThread in solveThreads)
            {
                solveThread.Join();
            }
            watch.Stop();
            Console.WriteLine("{0:F4} сек. Кол-во потоков: {1}. Приоритет: {2}", watch.Elapsed.TotalSeconds, threadCount, PriorityToString(priority));
        }