/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
- using System.Text;
-
- namespace Delta.Utilities.Profiling
- {
- /// <summary>
- /// Interface for Time sources. All TimeSource should start at tick 0 after
- /// the constructor was called.
- /// </summary>
- public abstract class BaseTimeSource
- {
- #region ElapsedMilliseconds (Public)
- /// <summary>
- /// Elapsed milliseconds as a long value. Can be converted quite easily
- /// from the ticks values times 1000 divided by the timer frequency.
- /// </summary>
- public long ElapsedMilliseconds;
- #endregion
-
- #region LastMilliseconds (Public)
- /// <summary>
- /// Last frame milliseconds value. Used for many checks if some time
- /// interval has passed. The Time.Delta value is even more accurate
- /// because it is a float value and can be very small.
- /// </summary>
- public long LastMilliseconds;
- #endregion
-
- #region ElapsedSeconds (Public)
- /// <summary>
- /// Total seconds that have passed of the application time. Use
- /// ElapsedMilliseconds for more accurate application timings.
- /// </summary>
- public int ElapsedSeconds;
- #endregion
-
- #region LastSeconds (Public)
- /// <summary>
- /// Last frame elapsed seconds. Used for some quick checks if a second
- /// has passed.
- /// </summary>
- public int LastSeconds;
- #endregion
-
- #region Private
-
- #region stringBuilder (Private)
- /// <summary>
- /// The time string is usually 6 letters long like 03.498, but can go up
- /// to 9 or 10 for big times like 59:03.458 or 643:49.584. But we allow
- /// much bigger messages for Log.FormatMessage.
- /// </summary>
- private readonly StringBuilder stringBuilder = new StringBuilder(80);
- #endregion
-
- #endregion
-
- #region Update (Public)
- /// <summary>
- /// Update the current ElapsedTicks and ElapsedMilliseconds values and
- /// return the difference to the last time this method was called as a
- /// floating point value in seconds.
- /// </summary>
- /// <returns>Difference to the last time this method was called, should
- /// never be null (we might devide through this value).</returns>
- public abstract float Update();
- #endregion
-
- #region GetExactTotalTimeInSecondsToday (Public)
- /// <summary>
- /// Reports a very accurate value rounded to a float in seconds for today.
- /// Warning: This method is slow, try to use Time.Seconds,
- /// Time.Milliseconds, Time.Delta, etc. instead if possible (much faster).
- /// The return value is in seconds and reports a fraction for up to 0.1ns.
- /// The value will be reset every day to stay accurate (again, after 3
- /// days floats lose accuracy and will not be very useful anymore). Used
- /// for profilers and more accurate input for example. If you do not need
- /// this high accuracy please only use the Milliseconds value or the helper
- /// methods here (all millisecond exact and updated once per frame).
- /// </summary>
- /// <returns>Time in seconds today as a float value, not very accurate,
- /// but good enough for a day (after 20-40 days it gets really inaccurate)
- /// </returns>
- public abstract float GetExactTotalTimeInSecondsToday();
- #endregion
-
- #region ToString (Public)
- /// <summary>
- /// To string helper method to display the total time in the format:
- /// [Minutes optional:]Seconds:Milliseconds, which is used for logging,
- /// but also useful for debugging and profiling.
- /// </summary>
- /// <returns>String with the time in the format
- /// Minutes:Seconds:Milliseconds</returns>
- public override string ToString()
- {
- // Own time from start in ms (way more accurate than DateTime)
- float time = GetExactTotalTimeInSecondsToday();
- int minutes = (int)(time / 60);
- int seconds = (int)(time) % 60;
- int ms = (int)(time * 1000) % 1000;
- // Note: Not very optimized (because string length is fixed, we could
- // do this even better), but fast enough! Skip minutes if at 00:xx
- stringBuilder.Length = 0;
- if (minutes > 0)
- {
- stringBuilder.Append(minutes.ToString("00"));
- stringBuilder.Append(':');
- }
- stringBuilder.Append(seconds.ToString("00"));
- stringBuilder.Append('.');
- stringBuilder.Append(ms.ToString("00"));
- return stringBuilder.ToString();
- }
- #endregion
-
- #region ToTimeString (Public)
- /// <summary>
- /// To string helper method to display the total time in the format:
- /// [Hour optional:][Minutes optional:]Seconds:Milliseconds, which is used
- /// for logging, but also useful for debugging and profiling.
- /// </summary>
- /// <param name="message">Message to be written after the time stamp
- /// </param>
- /// <param name="separator">Separator between the time stamp and the
- /// message (e.g. ": ")</param>
- /// <returns>String with the time in the format
- /// [optional Hour:][optional Minutes:]Seconds:Milliseconds</returns>
- public string ToTimeString(char separator, string message)
- {
- // Own time from start in ms (way more accurate than DateTime)
- float time = GetExactTotalTimeInSecondsToday();
- int minutes = (int)(time / 60);
- int seconds = (int)(time) % 60;
- int ms = (int)(time * 1000) % 1000;
- // Note: Not very optimized (because string length is fixed, we could
- // do this even better), but fast enough! Skip minutes if at 00:xx
- stringBuilder.Length = 0;
- if (minutes > 60)
- {
- int hours = minutes / 60;
- minutes = minutes % 60;
- stringBuilder.Append(hours.ToString("00"));
- stringBuilder.Append(':');
- stringBuilder.Append(minutes.ToString("00"));
- stringBuilder.Append(':');
- }
- else if (minutes > 0)
- {
- stringBuilder.Append(minutes.ToString("00"));
- stringBuilder.Append(':');
- }
- stringBuilder.Append(seconds.ToString("00"));
- stringBuilder.Append('.');
- stringBuilder.Append(ms.ToString("000"));
- // And add the original message
- stringBuilder.Append(separator);
- stringBuilder.Append(message);
- return stringBuilder.ToString();
- }
- #endregion
- }
- }