thief-mission-viewer/project/code/TMV/Timing.cs

49 lines
1.1 KiB
C#
Raw Permalink Normal View History

2024-08-26 12:22:29 +00:00
using System;
2024-08-29 18:27:18 +00:00
using System.Collections.Generic;
2024-08-26 12:22:29 +00:00
using System.Diagnostics;
namespace KeepersCompound.TMV;
public static class Timing
{
2024-08-29 18:27:18 +00:00
static readonly Dictionary<string, TimeSpan> _stages = new();
public static void Reset()
{
_stages.Clear();
}
2024-08-26 12:22:29 +00:00
public static void TimeStage(string stagename, Action action)
{
var watch = Stopwatch.StartNew();
action();
watch.Stop();
2024-08-29 18:27:18 +00:00
AddOrIncrement(stagename, watch.Elapsed);
2024-08-26 12:22:29 +00:00
}
2024-08-26 13:03:37 +00:00
public static T TimeStage<T>(string stagename, Func<T> action)
{
var watch = Stopwatch.StartNew();
var value = action();
watch.Stop();
2024-08-29 18:27:18 +00:00
AddOrIncrement(stagename, watch.Elapsed);
2024-08-26 13:03:37 +00:00
return value;
}
2024-08-29 18:27:18 +00:00
public static void LogAll()
{
foreach (var (stagename, time) in _stages)
{
Godot.GD.Print($"[{stagename}]: {time:g}");
}
}
private static void AddOrIncrement(string stagename, TimeSpan elapsed)
{
if (_stages.TryGetValue(stagename, out var time))
{
elapsed += time;
}
_stages[stagename] = elapsed;
}
2024-08-26 12:22:29 +00:00
}