From eb45460f213e2a63a2efc4255562bbb3a60ea978 Mon Sep 17 00:00:00 2001 From: Parnic Date: Wed, 15 Dec 2021 10:51:34 -0600 Subject: [PATCH] Add ability to manually stop a timer This way I can have multiple timers in a given scope that measure different things without super awkward `using`s that end up scoping out variables that I need access to later. The timings get printed in reverse order, of course, but the conveniences provided outweight that minor flaw. --- src/Timer.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Timer.cs b/src/Timer.cs index 1390627..996306b 100644 --- a/src/Timer.cs +++ b/src/Timer.cs @@ -6,15 +6,27 @@ namespace aoc2021 { private readonly Stopwatch stopwatch = Stopwatch.StartNew(); private readonly string? name; + private bool stopped = false; public Timer(string? inName = null) { name = inName; } + public void Stop() + { + if (stopped) + { + return; + } + + stopwatch.Stop(); + stopped = true; + } + public void Dispose() { - stopwatch.Stop(); + Stop(); var (elapsed, unit) = ConvertElapsedToHumanReadable(); var color = "[31m"; if (unit == "us" || (unit == "ms" && elapsed < 10))