namespace Sandbox.Diagnostics;
///
/// A lightweight, high-resolution timer for performance measurement.
/// More efficient than with a simpler API.
///
///
/// var timer = FastTimer.StartNew();
/// // Do work...
/// Log.Info( $"Took {timer.ElapsedMilliSeconds}ms" );
///
public struct FastTimer
{
///
/// Creates and starts a new FastTimer.
///
/// A started FastTimer
public static FastTimer StartNew()
{
var ft = new FastTimer();
ft.Start();
return ft;
}
long startTimestamp;
///
/// Starts or restarts the timer.
///
public void Start()
{
startTimestamp = Stopwatch.GetTimestamp();
}
///
/// Gets the timestamp when the timer was started.
///
public readonly long StartTick => startTimestamp;
///
/// Gets the number of ticks elapsed since the timer was started.
///
public readonly long ElapsedTicks => (Stopwatch.GetTimestamp() - startTimestamp);
///
/// Gets the number of microseconds elapsed since the timer was started.
///
public readonly double ElapsedMicroSeconds => ElapsedTicks * (1_000_000.0 / Stopwatch.Frequency);
///
/// Gets the number of milliseconds elapsed since the timer was started.
///
public readonly double ElapsedMilliSeconds => ElapsedTicks * (1_000.0 / Stopwatch.Frequency);
///
/// Gets the number of seconds elapsed since the timer was started.
///
public readonly double ElapsedSeconds => ElapsedTicks * (1.0 / Stopwatch.Frequency);
///
/// Gets the time elapsed since the timer was started as a TimeSpan.
///
public readonly TimeSpan Elapsed => TimeSpan.FromTicks( ElapsedTicks );
}