PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 35ms app.codeStats 0ms

/Utilities/Profiling/BaseTimeSource.cs

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