From 3c48484870759476c56a48a77b2b7b16ccc8a281 Mon Sep 17 00:00:00 2001 From: stefnotch Date: Sun, 6 Jun 2021 21:52:29 +0200 Subject: [PATCH] Use Stopwatch instead of DateTime.Now for increased accuracy DateTime.Now is decent, but not accurate enough for timings involving only a few milliseconds. --- Source/Tools/Flax.Build/Build/Profiling.cs | 7 +++++-- Source/Tools/Flax.Build/Program.cs | 8 ++++---- Source/Tools/Flax.Build/Utilities/Utilities.cs | 6 +++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Source/Tools/Flax.Build/Build/Profiling.cs b/Source/Tools/Flax.Build/Build/Profiling.cs index c5cc89bcb..785badad3 100644 --- a/Source/Tools/Flax.Build/Build/Profiling.cs +++ b/Source/Tools/Flax.Build/Build/Profiling.cs @@ -5,6 +5,7 @@ using System.IO; using System.Collections.Generic; using System.Text; using System.Threading; +using System.Diagnostics; namespace Flax.Build { @@ -71,6 +72,8 @@ namespace Flax.Build private static int _depth; private static readonly List _events = new List(1024); + private static readonly DateTime _startTime = DateTime.Now; + private static readonly Stopwatch _stopwatch = Stopwatch.StartNew(); // https://stackoverflow.com/questions/1416139/how-to-get-timestamp-of-tick-precision-in-net-c /// /// Begins the profiling event. @@ -81,7 +84,7 @@ namespace Flax.Build { Event e; e.Name = name; - e.StartTime = DateTime.Now; + e.StartTime = _startTime.AddTicks(_stopwatch.Elapsed.Ticks); e.Duration = TimeSpan.Zero; e.Depth = _depth++; e.ThreadId = Thread.CurrentThread.ManagedThreadId; @@ -95,7 +98,7 @@ namespace Flax.Build /// The event identifier returned by . public static void End(int id) { - var endTime = DateTime.Now; + var endTime = _startTime.AddTicks(_stopwatch.Elapsed.Ticks); var e = _events[id]; e.Duration = endTime - e.StartTime; _events[id] = e; diff --git a/Source/Tools/Flax.Build/Program.cs b/Source/Tools/Flax.Build/Program.cs index 89e71ace0..7411edb3b 100644 --- a/Source/Tools/Flax.Build/Program.cs +++ b/Source/Tools/Flax.Build/Program.cs @@ -1,6 +1,7 @@ // Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. using System; +using System.Diagnostics; using System.IO; using System.Net; using System.Threading; @@ -25,7 +26,7 @@ namespace Flax.Build } Mutex singleInstanceMutex = null; - var startTime = DateTime.Now; + Stopwatch stopwatch = Stopwatch.StartNew(); bool failed = false; try @@ -166,9 +167,8 @@ namespace Flax.Build singleInstanceMutex.Dispose(); singleInstanceMutex = null; } - - var endTime = DateTime.Now; - Log.Info(string.Format("Total time: {0}", endTime - startTime)); + stopwatch.Stop(); + Log.Info(string.Format("Total time: {0}", stopwatch.Elapsed)); Log.Verbose("End."); Log.Dispose(); } diff --git a/Source/Tools/Flax.Build/Utilities/Utilities.cs b/Source/Tools/Flax.Build/Utilities/Utilities.cs index 8390377a8..e08755175 100644 --- a/Source/Tools/Flax.Build/Utilities/Utilities.cs +++ b/Source/Tools/Flax.Build/Utilities/Utilities.cs @@ -326,7 +326,7 @@ namespace Flax.Build } } - var startTime = DateTime.UtcNow; + Stopwatch stopwatch = Stopwatch.StartNew(); if (!options.HasFlag(RunOptions.NoLoggingOfRunCommand)) { Log.Verbose("Running: " + app + " " + (string.IsNullOrEmpty(commandLine) ? "" : commandLine)); @@ -397,11 +397,11 @@ namespace Flax.Build if (!options.HasFlag(RunOptions.NoWaitForExit)) { - var buildDuration = (DateTime.UtcNow - startTime).TotalMilliseconds; + stopwatch.Stop(); result = proc.ExitCode; if (!options.HasFlag(RunOptions.NoLoggingOfRunCommand) || options.HasFlag(RunOptions.NoLoggingOfRunDuration)) { - Log.Info(string.Format("Took {0}s to run {1}, ExitCode={2}", buildDuration / 1000, Path.GetFileName(app), result)); + Log.Info(string.Format("Took {0}s to run {1}, ExitCode={2}", stopwatch.Elapsed.TotalSeconds, Path.GetFileName(app), result)); } }