Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Design;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text.RegularExpressions;
  10.  
  11. namespace Memory.Timers
  12. {
  13. public class Timer : IDisposable
  14. {
  15. private static string[] report;
  16.  
  17. public static string Report => string.Join("", report) + MakeRest();
  18.  
  19. private static string MakeRest()
  20. {
  21. var result = "";
  22. var rep = report.Where(z => z != null).ToList();
  23. var mainUsing = Convert.ToInt64(Regex.Replace(report[0], @"\D", ""));
  24. for (int i = rep.Count - 1; i > 0; i--)
  25. {
  26. var time = 0L;
  27. var splitted = rep[i].Split('\n').Where(z => z.Length > 0);
  28. foreach (var item in splitted)
  29. time += Convert.ToInt64(Regex.Replace(item, @"\D", ""));
  30.  
  31. result += MakeReport(i * 4, "Rest", mainUsing - time);
  32. }
  33. return result;
  34. }
  35.  
  36. private static readonly List<string> activeTimers = new List<string>();
  37.  
  38. private readonly Stopwatch timer;
  39.  
  40. private readonly string name;
  41.  
  42. private bool isDisposed = false;
  43.  
  44. public Timer(string name)
  45. {
  46. this.name = name;
  47. this.timer = new Stopwatch();
  48. this.timer.Start();
  49. }
  50.  
  51. public static IDisposable Start(string timer = "*")
  52. {
  53. if (activeTimers.Count == 0)
  54. report = new string[10];
  55.  
  56. activeTimers.Add(timer);
  57. return new Timer(timer);
  58. }
  59.  
  60. ~Timer() => Dispose(false);
  61.  
  62. public void Dispose()
  63. {
  64. Dispose(true);
  65. GC.SuppressFinalize(this);
  66. }
  67.  
  68. protected virtual void Dispose(bool fromDisposeMethod)
  69. {
  70. if (!isDisposed)
  71. {
  72. if (fromDisposeMethod)
  73. {
  74. var indexOfName = activeTimers.IndexOf(name);
  75. timer.Stop();
  76. var offset = 4 * indexOfName;
  77. report[indexOfName] += MakeReport(offset, name, timer.ElapsedMilliseconds);
  78. activeTimers.Remove(name);
  79. }
  80. isDisposed = true;
  81. }
  82. }
  83.  
  84. private static string MakeReport(int offset, string name, long time)
  85. {
  86. var result = new StringBuilder();
  87. result.Append(' ', offset);
  88. result.Append(name);
  89. result.Append(' ', 20 - name.Length - offset);
  90. result.Append(": " + time + '\n');
  91. return result.ToString();
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement