Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Threading.Tasks;
- namespace Utils
- {
- class Bench
- {
- private readonly Action Method1;
- private readonly Action Method2;
- private readonly int Cursor;
- private int total1;
- private int total2;
- public Bench(Action method1, Action method2) : this(method1, method2, 6) { }
- public Bench(Action method1, Action method2, int power)
- {
- Method1 = method1;
- Method2 = method2;
- Cursor = (int)Math.Pow(10, power);
- Method1.Invoke();
- Method2.Invoke();
- }
- public void Recur(int threadCount)
- {
- while (true)
- {
- Go(threadCount, true);
- Console.ReadLine();
- Console.WriteLine();
- }
- }
- public void Go(int threadCount, bool register)
- {
- int value = AutoGo(threadCount, register);
- if (register)
- {
- switch (value)
- {
- case 1:
- total1++;
- break;
- case 2:
- total2++;
- break;
- }
- Console.WriteLine("Method1: {0}", total1);
- Console.WriteLine("Method2: {0}", total2);
- }
- }
- private int AutoGo(int threadCount, bool register)
- {
- double d1;
- double d2;
- if (threadCount == 0)
- {
- d1 = Execute1();
- d2 = Execute2();
- }
- else
- {
- d1 = AutoGo(threadCount, Execute1);
- d2 = AutoGo(threadCount, Execute2);
- }
- if (register)
- {
- Console.WriteLine(d1.ToString() + (d1 < d2 ? " *" : ""));
- Console.WriteLine(d2.ToString() + (d2 < d1 ? " *" : ""));
- }
- return (d1 == d2 ? 0 : (d1 < d2 ? 1 : 2));
- }
- private double AutoGo(int threadCount, Func<double> method)
- {
- List<double> laps = new List<double>(threadCount);
- Task<double>[] array = new Task<double>[threadCount];
- TaskFactory<double> factory = new TaskFactory<double>();
- for (int i = 0; i < threadCount; i++)
- {
- array[i] = factory.StartNew(method);
- }
- for (int i = 0; i < threadCount; i++)
- {
- laps.Add(array[i].Result);
- }
- return laps.Average();
- }
- private double Execute1() => Execute(Method1);
- private double Execute2() => Execute(Method2);
- private double Execute(Action action)
- {
- List<long> laps = new List<long>(Cursor);
- Stopwatch watch = new Stopwatch();
- int bound = Cursor;
- for (int i = 0; i < bound; i++)
- {
- watch.Start();
- action.Invoke();
- watch.Stop();
- laps.Add(watch.ElapsedTicks);
- watch.Reset();
- }
- return laps.Average();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement