using System.Diagnostics; using System.Text; public class StepTimer { private string _name; private Stopwatch _sw; private int _stepNum; private bool _silent; private StringBuilder _sb; private long _lastMs; public static StepTimer StartNew(string name, bool silent = false) { return new StepTimer(name, silent); } private StepTimer(string name, bool silent) { #if DEBUG if (!_silent) { _name = name; _stepNum = 1; _silent = silent; _sb = new StringBuilder(); _sb.AppendLine($"StepTimer '{_name}'"); _lastMs = 0; _sw = Stopwatch.StartNew(); } #endif } public void EndStep(string label = "") { #if DEBUG if (_silent) return; var ms = _sw.ElapsedMilliseconds - _lastMs; _sb.AppendLine($"{_stepNum:00},{ms/1000.0:0.000},{label.Replace(",",";")}"); _stepNum++; _lastMs = _sw.ElapsedMilliseconds; #endif } public void Done() { #if DEBUG if (_silent) return; _sw.Stop(); var doubleLine = "=".PadRight(100, '='); Debug.Print(string.Empty); Debug.Print(doubleLine); Debug.Print(_sb.ToString()); Debug.Print($"TOTAL ELAPSED TIME = {_sw.ElapsedMilliseconds / 1000.0:0.000}s"); Debug.Print(doubleLine); Debug.Print(string.Empty); #endif } }