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.
This commit is contained in:
2021-12-15 10:51:34 -06:00
parent 5f830051e8
commit eb45460f21

View File

@ -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))