Add timing class from TMV

This commit is contained in:
Jarrod Doyle 2024-09-23 16:56:47 +01:00
parent d5664b2614
commit 94286e134f
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,47 @@
using System.Diagnostics;
namespace KeepersCompound.Lightmapper;
public static class Timing
{
static readonly Dictionary<string, TimeSpan> _stages = new();
public static void Reset()
{
_stages.Clear();
}
public static void TimeStage(string stagename, Action action)
{
var watch = Stopwatch.StartNew();
action();
watch.Stop();
AddOrIncrement(stagename, watch.Elapsed);
}
public static T TimeStage<T>(string stagename, Func<T> action)
{
var watch = Stopwatch.StartNew();
var value = action();
watch.Stop();
AddOrIncrement(stagename, watch.Elapsed);
return value;
}
public static void LogAll()
{
foreach (var (stagename, time) in _stages)
{
Console.WriteLine($"[{stagename}]: {time:g}");
}
}
private static void AddOrIncrement(string stagename, TimeSpan elapsed)
{
if (_stages.TryGetValue(stagename, out var time))
{
elapsed += time;
}
_stages[stagename] = elapsed;
}
}