Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Utils
  8. {
  9. class Bench
  10. {
  11. private readonly Action Method1;
  12. private readonly Action Method2;
  13. private readonly int Cursor;
  14.  
  15. private int total1;
  16. private int total2;
  17.  
  18. public Bench(Action method1, Action method2) : this(method1, method2, 6) { }
  19.  
  20. public Bench(Action method1, Action method2, int power)
  21. {
  22. Method1 = method1;
  23. Method2 = method2;
  24. Cursor = (int)Math.Pow(10, power);
  25.  
  26. Method1.Invoke();
  27. Method2.Invoke();
  28. }
  29.  
  30. public void Recur(int threadCount)
  31. {
  32. while (true)
  33. {
  34. Go(threadCount, true);
  35. Console.ReadLine();
  36. Console.WriteLine();
  37. }
  38. }
  39.  
  40. public void Go(int threadCount, bool register)
  41. {
  42. int value = AutoGo(threadCount, register);
  43.  
  44. if (register)
  45. {
  46. switch (value)
  47. {
  48. case 1:
  49. total1++;
  50. break;
  51.  
  52. case 2:
  53. total2++;
  54. break;
  55. }
  56.  
  57. Console.WriteLine("Method1: {0}", total1);
  58. Console.WriteLine("Method2: {0}", total2);
  59. }
  60. }
  61.  
  62. private int AutoGo(int threadCount, bool register)
  63. {
  64. double d1;
  65. double d2;
  66.  
  67. if (threadCount == 0)
  68. {
  69. d1 = Execute1();
  70. d2 = Execute2();
  71. }
  72. else
  73. {
  74. d1 = AutoGo(threadCount, Execute1);
  75. d2 = AutoGo(threadCount, Execute2);
  76. }
  77.  
  78. if (register)
  79. {
  80. Console.WriteLine(d1.ToString() + (d1 < d2 ? " *" : ""));
  81. Console.WriteLine(d2.ToString() + (d2 < d1 ? " *" : ""));
  82. }
  83.  
  84. return (d1 == d2 ? 0 : (d1 < d2 ? 1 : 2));
  85. }
  86.  
  87. private double AutoGo(int threadCount, Func<double> method)
  88. {
  89. List<double> laps = new List<double>(threadCount);
  90. Task<double>[] array = new Task<double>[threadCount];
  91. TaskFactory<double> factory = new TaskFactory<double>();
  92.  
  93. for (int i = 0; i < threadCount; i++)
  94. {
  95. array[i] = factory.StartNew(method);
  96. }
  97.  
  98. for (int i = 0; i < threadCount; i++)
  99. {
  100. laps.Add(array[i].Result);
  101. }
  102.  
  103. return laps.Average();
  104. }
  105.  
  106. private double Execute1() => Execute(Method1);
  107. private double Execute2() => Execute(Method2);
  108.  
  109. private double Execute(Action action)
  110. {
  111. List<long> laps = new List<long>(Cursor);
  112. Stopwatch watch = new Stopwatch();
  113.  
  114. int bound = Cursor;
  115.  
  116. for (int i = 0; i < bound; i++)
  117. {
  118. watch.Start();
  119. action.Invoke();
  120. watch.Stop();
  121. laps.Add(watch.ElapsedTicks);
  122. watch.Reset();
  123. }
  124.  
  125. return laps.Average();
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement