diff --git a/Runtime/Timer.cs b/Runtime/Timer.cs index 52201a9..a7fa411 100644 --- a/Runtime/Timer.cs +++ b/Runtime/Timer.cs @@ -4,14 +4,19 @@ using System.Diagnostics; using Debug = UnityEngine.Debug; + public enum TimeUnit { Milliseconds, Nanoseconds } + /// /// Basic timer that logs execution time of a method or part of the method. It does not warm up the execution and /// runs the actions only once. /// public static class Timer { + private const int NanosecondsInAMillisecond = 1000000; + /// Log time in ms the action took. /// Name of the action which execution is measured. + /// The time unit to use (ms, ns, etc.) /// Action to execute. /// /// LogTime("Show popup", () => @@ -20,12 +25,24 @@ public static class Timer /// dropdownWindow.ShowInPopup(); /// }); /// - public static void LogTime(string actionName, Action action) + public static void LogTime(string actionName, TimeUnit timeUnit, Action action) { var stopWatch = Stopwatch.StartNew(); action(); stopWatch.Stop(); - Debug.Log($"{actionName} took {Convert.ToInt32(stopWatch.ElapsedMilliseconds)} ms."); + + switch (timeUnit) + { + case TimeUnit.Milliseconds: + Debug.Log($"{actionName} took {Convert.ToInt32(stopWatch.ElapsedMilliseconds)} ms."); + break; + case TimeUnit.Nanoseconds: + Debug.Log($"{actionName} took {Convert.ToInt32(stopWatch.Elapsed.TotalMilliseconds * NanosecondsInAMillisecond)} ns."); + break; + default: + Debug.Log($"{nameof(LogTime)} does not have an implementation of the following time unit yet: {timeUnit}."); + break; + } } } } \ No newline at end of file