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:
14
src/Timer.cs
14
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))
|
||||
|
Reference in New Issue
Block a user