Timer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. namespace bbiwarg.Utility
  8. {
  9. /// <summary>
  10. /// Stores and prints timing information for different code sections.
  11. /// </summary>
  12. static class Timer
  13. {
  14. /// <summary>
  15. /// used to prevent running <see cref="start"/>, <see cref="stop"/> and <see cref="outputAll"/> simultaneously from different threads
  16. /// </summary>
  17. private static Object sync = new object();
  18. /// <summary>
  19. /// dictionary of stopwatches indexed by name of the code section
  20. /// </summary>
  21. private static Dictionary<String, Stopwatch> stopwatches = new Dictionary<string, Stopwatch>();
  22. /// <summary>
  23. /// dictionary of current runtimes indexed by name of the code section
  24. /// </summary>
  25. private static Dictionary<String, double> currentTimes = new Dictionary<string, double>();
  26. /// <summary>
  27. /// dictionary of minimum runtimes indexed by name of the code section
  28. /// </summary>
  29. private static Dictionary<String, double> minTimes = new Dictionary<string, double>();
  30. /// <summary>
  31. /// dictionary of maximum runtimes indexed by name of the code section
  32. /// </summary>
  33. private static Dictionary<String, double> maxTimes = new Dictionary<string, double>();
  34. /// <summary>
  35. /// dictionary of the sum of runtimes indexed by name of the code section
  36. /// </summary>
  37. private static Dictionary<String, double> sumTimes = new Dictionary<string, double>();
  38. /// <summary>
  39. /// dictionary of the number of times the time was measured indexed by name of the code section
  40. /// </summary>
  41. private static Dictionary<String, int> numTimes = new Dictionary<string, int>();
  42. /// <summary>
  43. /// the maximum length for the name of a code section
  44. /// </summary>
  45. private static int maxNameLength = 1;
  46. /// <summary>
  47. /// Starts a timer for the given name and initializes the dictionaries when called for the first time with this name.
  48. /// </summary>
  49. /// <param name="name">name of the code section</param>
  50. public static void start(String name)
  51. {
  52. lock (sync)
  53. {
  54. if (!stopwatches.ContainsKey(name))
  55. {
  56. stopwatches.Add(name, new Stopwatch());
  57. minTimes.Add(name, int.MaxValue);
  58. maxTimes.Add(name, 0);
  59. sumTimes.Add(name, 0);
  60. numTimes.Add(name, 0);
  61. currentTimes.Add(name, 0);
  62. maxNameLength = Math.Max(maxNameLength, name.Length);
  63. }
  64. stopwatches[name].Restart();
  65. }
  66. }
  67. /// <summary>
  68. /// Stops the timer for the given name and stores timing information.
  69. /// </summary>
  70. /// <param name="name">name of the code section</param>
  71. public static void stop(String name)
  72. {
  73. lock (sync)
  74. {
  75. stopwatches[name].Stop();
  76. double time = Math.Round((double)stopwatches[name].ElapsedTicks / (double)Stopwatch.Frequency * 1000.0, 2);
  77. if (time < minTimes[name]) minTimes[name] = time;
  78. if (time > maxTimes[name]) maxTimes[name] = time;
  79. sumTimes[name] += time;
  80. numTimes[name]++;
  81. currentTimes[name] = time;
  82. }
  83. }
  84. /// <summary>
  85. /// Prints all collected timing information.
  86. /// </summary>
  87. public static void outputAll()
  88. {
  89. lock (sync)
  90. {
  91. StringBuilder divider = new StringBuilder();
  92. divider.Append("├-");
  93. divider.Append(new String('-', maxNameLength));
  94. divider.Append("-┼-------┼-------┤");
  95. Console.Clear();
  96. Console.WriteLine(String.Format("| {0,-" + maxNameLength + "} | {1,-5} | {2,-5} |", "NAME", "AVG.", "CUR."));
  97. Console.WriteLine(divider.ToString());
  98. foreach (String name in stopwatches.Keys)
  99. {
  100. double average = sumTimes[name] / Math.Max(numTimes[name], 1);
  101. double current = currentTimes[name];
  102. Console.WriteLine(String.Format("| {0,-" + maxNameLength + "} | {1:00.00} | {2:00.00} |", name, average, current));
  103. }
  104. }
  105. }
  106. }
  107. }