Add ability to name timers

This commit is contained in:
2021-12-14 12:48:45 -06:00
parent 49e1e667a8
commit 07cad08733

View File

@ -5,6 +5,13 @@ namespace aoc2021
internal class Timer : IDisposable internal class Timer : IDisposable
{ {
private readonly Stopwatch stopwatch = Stopwatch.StartNew(); private readonly Stopwatch stopwatch = Stopwatch.StartNew();
private readonly string? name;
public Timer(string? inName = null)
{
name = inName;
}
public void Dispose() public void Dispose()
{ {
stopwatch.Stop(); stopwatch.Stop();
@ -18,7 +25,7 @@ namespace aoc2021
{ {
color = "[33m"; color = "[33m";
} }
Logger.Log($"Took \u001b{color}{elapsed:N1}{unit}\u001b[0m"); Logger.Log($"{name}{(!string.IsNullOrEmpty(name) ? " t" : "T")}ook \u001b{color}{elapsed:N1}{unit}\u001b[0m");
} }
public (double elapsed, string unit) ConvertElapsedToHumanReadable() public (double elapsed, string unit) ConvertElapsedToHumanReadable()