From 36ebeadddf8ccf6889a891d4dea8bf7f02b0cd30 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Mon, 3 Feb 2020 17:48:56 -0500 Subject: [PATCH] add Timer class for tracking draw & update times (etc) related issue: #33 GitOrigin-RevId: 3762a7a5c7becf83cc50f80d4e5bb6f27244a952 --- Shared/Timer.cs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Shared/Timer.cs diff --git a/Shared/Timer.cs b/Shared/Timer.cs new file mode 100644 index 0000000..ff6d66e --- /dev/null +++ b/Shared/Timer.cs @@ -0,0 +1,46 @@ +using System; +using System.Diagnostics; + +namespace SemiColinGames { + class Timer { + + private readonly Stopwatch stopwatch = new Stopwatch(); + private readonly double targetTime; + private readonly string name; + private double startTime = 0.0; + private int[] histogram = new int[41]; + private int totalFrames = 0; + + public Timer(double targetTime, string name) { + this.targetTime = targetTime; + this.name = name; + } + + public void Start() { + startTime = stopwatch.Elapsed.TotalSeconds; + stopwatch.Start(); + } + + public void Stop() { + stopwatch.Stop(); + totalFrames++; + double frameTime = stopwatch.Elapsed.TotalSeconds - startTime; + int bucket = FMath.Clamp((int) (5.0f * frameTime / targetTime), 0, 40); + histogram[bucket]++; + if (totalFrames % 100 == 0) { + DumpStats(); + } + } + + public void DumpStats() { + Debug.WriteLine(name + ".DumpStats():"); + for (int i = 0; i < histogram.Length - 1; i++) { + int value = histogram[i]; + if (value > 0) { + Debug.WriteLine($"{i * 5}-{(i + 1) * 5}%: {histogram[i]}"); + } + } + Debug.WriteLine($"200+%: {histogram[histogram.Length - 1]}"); + } + } +}