PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/Profiling/BaseTimeSource.cs

#
C# | 162 lines | 79 code | 11 blank | 72 comment | 4 complexity | b1f87e9c42a2f8abf99b1b266129027e MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Text;
  2. namespace Delta.Utilities.Profiling
  3. {
  4. /// <summary>
  5. /// Interface for Time sources. All TimeSource should start at tick 0 after
  6. /// the constructor was called.
  7. /// </summary>
  8. public abstract class BaseTimeSource
  9. {
  10. #region ElapsedMilliseconds (Public)
  11. /// <summary>
  12. /// Elapsed milliseconds as a long value. Can be converted quite easily
  13. /// from the ticks values times 1000 divided by the timer frequency.
  14. /// </summary>
  15. public long ElapsedMilliseconds;
  16. #endregion
  17. #region LastMilliseconds (Public)
  18. /// <summary>
  19. /// Last frame milliseconds value. Used for many checks if some time
  20. /// interval has passed. The Time.Delta value is even more accurate
  21. /// because it is a float value and can be very small.
  22. /// </summary>
  23. public long LastMilliseconds;
  24. #endregion
  25. #region ElapsedSeconds (Public)
  26. /// <summary>
  27. /// Total seconds that have passed of the application time. Use
  28. /// ElapsedMilliseconds for more accurate application timings.
  29. /// </summary>
  30. public int ElapsedSeconds;
  31. #endregion
  32. #region LastSeconds (Public)
  33. /// <summary>
  34. /// Last frame elapsed seconds. Used for some quick checks if a second
  35. /// has passed.
  36. /// </summary>
  37. public int LastSeconds;
  38. #endregion
  39. #region Private
  40. #region stringBuilder (Private)
  41. /// <summary>
  42. /// The time string is usually 6 letters long like 03.498, but can go up
  43. /// to 9 or 10 for big times like 59:03.458 or 643:49.584. But we allow
  44. /// much bigger messages for Log.FormatMessage.
  45. /// </summary>
  46. private readonly StringBuilder stringBuilder = new StringBuilder(80);
  47. #endregion
  48. #endregion
  49. #region Update (Public)
  50. /// <summary>
  51. /// Update the current ElapsedTicks and ElapsedMilliseconds values and
  52. /// return the difference to the last time this method was called as a
  53. /// floating point value in seconds.
  54. /// </summary>
  55. /// <returns>Difference to the last time this method was called, should
  56. /// never be null (we might devide through this value).</returns>
  57. public abstract float Update();
  58. #endregion
  59. #region GetExactTotalTimeInSecondsToday (Public)
  60. /// <summary>
  61. /// Reports a very accurate value rounded to a float in seconds for today.
  62. /// Warning: This method is slow, try to use Time.Seconds,
  63. /// Time.Milliseconds, Time.Delta, etc. instead if possible (much faster).
  64. /// The return value is in seconds and reports a fraction for up to 0.1ns.
  65. /// The value will be reset every day to stay accurate (again, after 3
  66. /// days floats lose accuracy and will not be very useful anymore). Used
  67. /// for profilers and more accurate input for example. If you do not need
  68. /// this high accuracy please only use the Milliseconds value or the helper
  69. /// methods here (all millisecond exact and updated once per frame).
  70. /// </summary>
  71. /// <returns>Time in seconds today as a float value, not very accurate,
  72. /// but good enough for a day (after 20-40 days it gets really inaccurate)
  73. /// </returns>
  74. public abstract float GetExactTotalTimeInSecondsToday();
  75. #endregion
  76. #region ToString (Public)
  77. /// <summary>
  78. /// To string helper method to display the total time in the format:
  79. /// [Minutes optional:]Seconds:Milliseconds, which is used for logging,
  80. /// but also useful for debugging and profiling.
  81. /// </summary>
  82. /// <returns>String with the time in the format
  83. /// Minutes:Seconds:Milliseconds</returns>
  84. public override string ToString()
  85. {
  86. // Own time from start in ms (way more accurate than DateTime)
  87. float time = GetExactTotalTimeInSecondsToday();
  88. int minutes = (int)(time / 60);
  89. int seconds = (int)(time) % 60;
  90. int ms = (int)(time * 1000) % 1000;
  91. // Note: Not very optimized (because string length is fixed, we could
  92. // do this even better), but fast enough! Skip minutes if at 00:xx
  93. stringBuilder.Length = 0;
  94. if (minutes > 0)
  95. {
  96. stringBuilder.Append(minutes.ToString("00"));
  97. stringBuilder.Append(':');
  98. }
  99. stringBuilder.Append(seconds.ToString("00"));
  100. stringBuilder.Append('.');
  101. stringBuilder.Append(ms.ToString("00"));
  102. return stringBuilder.ToString();
  103. }
  104. #endregion
  105. #region ToTimeString (Public)
  106. /// <summary>
  107. /// To string helper method to display the total time in the format:
  108. /// [Hour optional:][Minutes optional:]Seconds:Milliseconds, which is used
  109. /// for logging, but also useful for debugging and profiling.
  110. /// </summary>
  111. /// <param name="message">Message to be written after the time stamp
  112. /// </param>
  113. /// <param name="separator">Separator between the time stamp and the
  114. /// message (e.g. ": ")</param>
  115. /// <returns>String with the time in the format
  116. /// [optional Hour:][optional Minutes:]Seconds:Milliseconds</returns>
  117. public string ToTimeString(char separator, string message)
  118. {
  119. // Own time from start in ms (way more accurate than DateTime)
  120. float time = GetExactTotalTimeInSecondsToday();
  121. int minutes = (int)(time / 60);
  122. int seconds = (int)(time) % 60;
  123. int ms = (int)(time * 1000) % 1000;
  124. // Note: Not very optimized (because string length is fixed, we could
  125. // do this even better), but fast enough! Skip minutes if at 00:xx
  126. stringBuilder.Length = 0;
  127. if (minutes > 60)
  128. {
  129. int hours = minutes / 60;
  130. minutes = minutes % 60;
  131. stringBuilder.Append(hours.ToString("00"));
  132. stringBuilder.Append(':');
  133. stringBuilder.Append(minutes.ToString("00"));
  134. stringBuilder.Append(':');
  135. }
  136. else if (minutes > 0)
  137. {
  138. stringBuilder.Append(minutes.ToString("00"));
  139. stringBuilder.Append(':');
  140. }
  141. stringBuilder.Append(seconds.ToString("00"));
  142. stringBuilder.Append('.');
  143. stringBuilder.Append(ms.ToString("000"));
  144. // And add the original message
  145. stringBuilder.Append(separator);
  146. stringBuilder.Append(message);
  147. return stringBuilder.ToString();
  148. }
  149. #endregion
  150. }
  151. }