From 783fd8eb32fa69558a30cd748af8a9408071b786 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Tue, 10 Jun 2014 21:11:35 +0100 Subject: [PATCH] Streamline PerfSample. - Avoid memory allocations by making PerfSample a struct, and tracking ticks manually rather than creating a Stopwatch instance. --- OpenRA.Game/Support/PerfHistory.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/OpenRA.Game/Support/PerfHistory.cs b/OpenRA.Game/Support/PerfHistory.cs index 4f9c00f32f..3850d83932 100644 --- a/OpenRA.Game/Support/PerfHistory.cs +++ b/OpenRA.Game/Support/PerfHistory.cs @@ -105,19 +105,20 @@ namespace OpenRA.Support } } - public sealed class PerfSample : IDisposable + public struct PerfSample : IDisposable { - readonly Stopwatch sw = Stopwatch.StartNew(); - readonly string Item; + readonly string item; + readonly long ticks; public PerfSample(string item) { - Item = item; + this.item = item; + ticks = Stopwatch.GetTimestamp(); } public void Dispose() { - PerfHistory.Increment(Item, sw.Elapsed.TotalMilliseconds); + PerfHistory.Increment(item, 1000.0 * Math.Max(0, Stopwatch.GetTimestamp() - ticks) / Stopwatch.Frequency); } } }