mirror of
https://github.com/parnic/advent-of-code-2024.git
synced 2025-06-16 20:40:14 -05:00
49 lines
1019 B
C#
49 lines
1019 B
C#
using System.Diagnostics;
|
|
|
|
namespace aoc2024;
|
|
|
|
internal class Timer : IDisposable
|
|
{
|
|
private readonly Stopwatch stopwatch = Stopwatch.StartNew();
|
|
private readonly string? name;
|
|
private bool stopped;
|
|
public bool Disabled { get; set; }
|
|
|
|
public Timer(string? inName = null)
|
|
{
|
|
name = inName;
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (stopped)
|
|
{
|
|
return;
|
|
}
|
|
|
|
stopwatch.Stop();
|
|
stopped = true;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Stop();
|
|
if (Disabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var (elapsed, unit) = stopwatch.ConvertToHumanReadable();
|
|
var color = "<red>";
|
|
if (unit == "us" || (unit == "ms" && elapsed < 10))
|
|
{
|
|
color = "<green>";
|
|
}
|
|
else if (unit == "ms" && elapsed < 250)
|
|
{
|
|
color = "<yellow>";
|
|
}
|
|
Logger.LogLine($"<cyan>{name}{(!string.IsNullOrEmpty(name) ? " t" : "T")}ook {color}{elapsed:N1}{unit}<r>");
|
|
}
|
|
}
|