Guest User

Untitled

a guest
Mar 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. 1000 ms for DateTime.Now.Ticks
  2. 0201 ms for Stopwatch.ElapsedTicks
  3. 0142 ms for Stopwatch.ElapsedMilliseconds
  4. 0139 ms for Stopwatch.ElapsedTicks after Reset
  5. 0264 ms for Stopwatch.ElapsedTicks setting ThreadAffinity
  6. 0151 ms for Stopwatch.ElapsedTicks setting ProcessorAffinity (and more)
  7. 0371 ms for Stopwatch.ElapsedTicks with Syncronized object
  8. Done!
  9.  
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. StopWatchTest.Go();
  15. Console.WriteLine("Done!");
  16. Console.ReadLine();
  17. }
  18. }
  19.  
  20. internal static class StopWatchTest
  21. {
  22. public const int SleepTime = 1000;
  23.  
  24. public static void Go()
  25. {
  26. #region Test #0 with DateTime.Now.Ticks
  27. long startTick = DateTime.Now.Ticks;
  28. Thread.Sleep(SleepTime);
  29. long stopTick = DateTime.Now.Ticks;
  30. long elapsedDt = (stopTick - startTick) * 100;
  31. Display((int)(elapsedDt / 1000 / 1000), "DateTime.Now.Ticks");
  32. #endregion Test #0 with DateTime.Now.Ticks
  33.  
  34. Stopwatch watch = Stopwatch.StartNew();
  35. long frequency = Stopwatch.Frequency;
  36. double nanosecPerTick = (1000.0 * 1000.0 * 1000.0) / frequency;
  37.  
  38. #region Test #1 with Stopwatch.ElapsedTicks
  39. startTick = watch.ElapsedTicks;
  40. Thread.Sleep(SleepTime);
  41. stopTick = watch.ElapsedTicks;
  42. double elapsedSw = (stopTick - startTick) * nanosecPerTick;
  43. Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks");
  44. #endregion Test #1 with Stopwatch.ElapsedTicks
  45.  
  46. #region Test #2 with Stopwatch.ElapsedMilliseconds
  47. startTick = watch.ElapsedMilliseconds;
  48. Thread.Sleep(SleepTime);
  49. stopTick = watch.ElapsedMilliseconds;
  50. Display((int)(stopTick - startTick), "Stopwatch.ElapsedMilliseconds");
  51. #endregion Test #2 with Stopwatch.ElapsedMilliseconds
  52.  
  53. #region Test #3 with Stopwatch.ElapsedTicks after Reset
  54. watch.Stop();
  55. watch.Reset();
  56. watch.Start();
  57. startTick = watch.ElapsedTicks;
  58. Thread.Sleep(SleepTime);
  59. stopTick = watch.ElapsedTicks;
  60. elapsedSw = (stopTick - startTick) * nanosecPerTick;
  61. Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks after Reset");
  62. #endregion Test #3 with Stopwatch.ElapsedTicks after Reset
  63.  
  64. #region Test #4 with Stopwatch.ElapsedTicks and ThreadAffinity
  65. Thread.BeginThreadAffinity();
  66. startTick = watch.ElapsedTicks;
  67. Thread.Sleep(SleepTime);
  68. stopTick = watch.ElapsedTicks;
  69. elapsedSw = (stopTick - startTick) * nanosecPerTick;
  70. Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks setting ThreadAffinity");
  71. Thread.EndThreadAffinity();
  72. #endregion Test #4 with Stopwatch.ElapsedTicks and ThreadAffinity
  73.  
  74. #region Test #5 with Stopwatch.ElapsedTicks and ProcessorAffinity (and more)
  75. const int affinity = 0x0001;
  76. Process proc = Process.GetCurrentProcess();
  77. proc.ProcessorAffinity = new IntPtr(affinity);
  78. proc.PriorityClass = ProcessPriorityClass.High;
  79. ProcessThreadCollection ptc = proc.Threads;
  80. foreach (ProcessThread pt in ptc)
  81. {
  82. pt.IdealProcessor = 0;
  83. pt.ProcessorAffinity = new IntPtr(affinity);
  84. }
  85. Thread.CurrentThread.Priority = ThreadPriority.Highest;
  86.  
  87. startTick = watch.ElapsedTicks;
  88. Thread.Sleep(SleepTime);
  89. stopTick = watch.ElapsedTicks;
  90. elapsedSw = (stopTick - startTick) * nanosecPerTick;
  91. Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks setting ProcessorAffinity (and more)");
  92. #endregion Test #5 with ProcessorAffinity and more
  93.  
  94. #region Test #6 with Syncronized object
  95. elapsedSw = new SyncTimer().Go();
  96. Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks with Syncronized object");
  97. #endregion Test #6 with Syncronized object
  98. }
  99.  
  100. private static void Display(int milliseconds, string testName)
  101. {
  102. Console.WriteLine("{0:0000} ms for {1}", milliseconds, testName);
  103. }
  104. }
  105.  
  106. [Synchronization]
  107. internal class SyncTimer : ContextBoundObject
  108. {
  109. [MethodImpl(MethodImplOptions.Synchronized)]
  110. public double Go()
  111. {
  112. Stopwatch.StartNew();
  113. long frequency = Stopwatch.Frequency;
  114. double nanosecPerTick = (1000.0 * 1000.0 * 1000.0) / frequency;
  115.  
  116. long startTick = Stopwatch.GetTimestamp();
  117. Thread.Sleep(StopWatchTest.SleepTime);
  118. long stopTick = Stopwatch.GetTimestamp();
  119. return (stopTick - startTick) * nanosecPerTick;
  120. }
  121. }
  122.  
  123. Stopwatch sw = new Stopwatch();
  124. sw.Start();
  125.  
  126. ... DO WORK
  127.  
  128. sw.Stop();
  129.  
  130. long elapsedTicks = sw.Elapsed.Ticks;
  131. Debug.WriteLine(TimeSpan.FromTicks(elapsedTicks).ToString());
  132.  
  133. 1000 ms for DateTime.Now.Ticks
  134. 0999 ms for Stopwatch.ElapsedTicks
  135. 1000 ms for Stopwatch.ElapsedMilliseconds
  136. 0999 ms for Stopwatch.ElapsedTicks after Reset
  137. 0999 ms for Stopwatch.ElapsedTicks setting ThreadAffinity
  138. 0999 ms for Stopwatch.ElapsedTicks setting ProcessorAffinity (and more)
  139.  
  140. var sw = new Stopwatch();
  141. sw.Start();
  142. Thread.Sleep(1000);
  143. var elpased = sw.Elapsed;
  144.  
  145. using System;
  146. using System.Diagnostics;
  147.  
  148. namespace HQ.Util.General
  149. {
  150. public static class StopWatchExtension
  151. {
  152. public static TimeSpan ToTimeSpan(this Stopwatch stopWatch)
  153. {
  154. return TimeSpan.FromTicks(stopWatch.ElapsedTicks);
  155. }
  156. }
  157. }
  158.  
  159. using HQ.Util.General;
  160.  
  161. Debug.Print($"Elapsed millisec: { stopWatch.ToTimeSpan().TotalMilliseconds}");
Add Comment
Please, Sign In to add comment