From 72f8666f5d2d88cc178031c8d911ce01c949e06e Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Thu, 29 Aug 2024 19:27:18 +0100 Subject: [PATCH] Sum repeated stage names --- project/code/TMV/Mission.cs | 2 ++ project/code/TMV/Timing.cs | 29 +++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/project/code/TMV/Mission.cs b/project/code/TMV/Mission.cs index c76096f..bccfbf3 100644 --- a/project/code/TMV/Mission.cs +++ b/project/code/TMV/Mission.cs @@ -74,7 +74,9 @@ public partial class Mission : Node3D { if (Build) { + Timing.Reset(); Timing.TimeStage("Build", () => RebuildMap()); + Timing.LogAll(); Build = false; } if (Clear) diff --git a/project/code/TMV/Timing.cs b/project/code/TMV/Timing.cs index 4cc1e69..5039b38 100644 --- a/project/code/TMV/Timing.cs +++ b/project/code/TMV/Timing.cs @@ -1,16 +1,24 @@ using System; +using System.Collections.Generic; using System.Diagnostics; namespace KeepersCompound.TMV; public static class Timing { + static readonly Dictionary _stages = new(); + + public static void Reset() + { + _stages.Clear(); + } + public static void TimeStage(string stagename, Action action) { var watch = Stopwatch.StartNew(); action(); watch.Stop(); - Godot.GD.Print($"[{stagename}]: {watch.Elapsed:g}"); + AddOrIncrement(stagename, watch.Elapsed); } public static T TimeStage(string stagename, Func action) @@ -18,7 +26,24 @@ public static class Timing var watch = Stopwatch.StartNew(); var value = action(); watch.Stop(); - Godot.GD.Print($"[{stagename}]: {watch.Elapsed:g}"); + AddOrIncrement(stagename, watch.Elapsed); return value; } + + 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; + } } \ No newline at end of file