PageRenderTime 308ms CodeModel.GetById 108ms app.highlight 3ms RepoModel.GetById 96ms app.codeStats 0ms

/Utilities/Profiling/StopwatchTimeSource.cs

#
C# | 102 lines | 48 code | 10 blank | 44 comment | 0 complexity | f17b39f496f9843468463cd0d0746d6c MD5 | raw file
  1using System.Diagnostics;
  2
  3namespace Delta.Utilities.Profiling
  4{
  5	/// <summary>
  6	/// Implementation of ITimeSource using C# Stopwatch class
  7	/// </summary>
  8	public class StopwatchTimeSource : BaseTimeSource
  9	{
 10		#region Private
 11
 12		#region stopwatch (Private)
 13		/// <summary>
 14		/// Internal stopwatch
 15		/// </summary>
 16		private readonly Stopwatch stopwatch;
 17		#endregion
 18
 19		#region timerFrequency (Private)
 20		/// <summary>
 21		/// Number of ticks per second
 22		/// </summary>
 23		private readonly long timerFrequency;
 24		#endregion
 25
 26		#region lastTicks (Private)
 27		/// <summary>
 28		/// Ticks from last frame, used to calculate Delta, which is more
 29		/// accurate than using just ElapsedMilliseconds.
 30		/// </summary>
 31		private long lastTicks;
 32		#endregion
 33
 34		#endregion
 35
 36		#region Constructors
 37		/// <summary>
 38		/// Create stopwatch time source
 39		/// </summary>
 40		public StopwatchTimeSource()
 41		{
 42			// Note: Stopwatch.IsHighResolution tells us if we are precise (true)
 43			stopwatch = new Stopwatch();
 44			timerFrequency = Stopwatch.Frequency;
 45			stopwatch.Start();
 46			lastTicks = stopwatch.ElapsedTicks;
 47		}
 48		#endregion
 49
 50		#region Update (Public)
 51		/// <summary>
 52		/// Update the current ElapsedTicks and ElapsedMilliseconds values and
 53		/// return the difference of the last time with a floating point value.
 54		/// </summary>
 55		/// <returns>Difference to the last time this method was called</returns>
 56		public override float Update()
 57		{
 58			// First copy over the last frame values
 59			LastSeconds = ElapsedSeconds;
 60			LastMilliseconds = ElapsedMilliseconds;
 61
 62			// Update Total and Delta times. This is fast, no extra checking needed
 63			long elapsedTicks = stopwatch.ElapsedTicks;
 64			ElapsedMilliseconds = elapsedTicks * 1000 / timerFrequency;
 65			//ElapsedSeconds = (int)(elapsedTicks / timerFrequency);
 66			//might be faster:
 67			ElapsedSeconds = (int)(ElapsedMilliseconds / 1000);
 68
 69			// And finally return the delta in seconds since the last frame.
 70			float delta = ((float)(elapsedTicks - lastTicks) / timerFrequency);
 71			lastTicks = elapsedTicks;
 72			return delta;
 73		}
 74		#endregion
 75
 76		#region GetExactTotalTimeInSecondsToday (Public)
 77		/// <summary>
 78		/// Reports a very accurate value rounded to a float in seconds for today.
 79		/// The return value is in seconds and reports a fraction for up to 0.1ns.
 80		/// The value will be reset every day to stay accurate (again, after 3
 81		/// days floats lose accuracy and will not be very useful anymore). Used
 82		/// for profilers and more accurate input for example. If you do not need
 83		/// this high accuracy please only use the Milliseconds value or the helper
 84		/// methods here (all millisecond exact and updated once per frame).
 85		/// </summary>
 86		/// <returns>Time in seconds today as a float value, not very accurate,
 87		/// but good enough for a day (after 20-40 days it gets really inaccurate)
 88		/// </returns>
 89		public override float GetExactTotalTimeInSecondsToday()
 90		{
 91			long elapsedTicks = stopwatch.ElapsedTicks;
 92			// How many ticks are in a day (ticks per second * seconds in a day)?
 93			long ticksInADay = timerFrequency * 60 * 60 * 24;
 94			// Now modulate the ticksInADay out, we do not want more than that.
 95			// Basically start over every 24 hours and have the time reset to 0 then.
 96			// This way the returned float value stays accurate not just for the
 97			// first few days, but millions of years :)
 98			return ((float)(elapsedTicks % ticksInADay) / timerFrequency);
 99		}
100		#endregion
101	}
102}