using System; using System.Diagnostics; namespace BizHawk.Common { /// Create a new instance of this class in a block, and it will measure the time elapsed until the block finishes executing. Provide a label to print to stdout or provide a callback for custom behaviour. public class SimpleTime : IDisposable { private readonly Action _callback; private readonly Stopwatch _stopwatch = new Stopwatch(); public SimpleTime(Action callback) { _callback = callback; _stopwatch.Start(); } public SimpleTime(string label) : this(l => Console.WriteLine($"Elapsed time for {label}: {l} ms")) {} public void Dispose() { _stopwatch.Stop(); _callback(_stopwatch.ElapsedMilliseconds); } } }