Last active
March 9, 2020 21:49
-
-
Save ogxd/abe8cd137834113f6cf6c59369bc4e7f to your computer and use it in GitHub Desktop.
Display elasped time in a time interval.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
public static class Profiling { | |
private static Dictionary<string, Stopwatch> stopwatches = new Dictionary<string, Stopwatch>(); | |
public static void Start(string key) { | |
if (!stopwatches.ContainsKey(key)) | |
stopwatches.Add(key, Stopwatch.StartNew()); | |
else { | |
stopwatches[key] = Stopwatch.StartNew(); | |
} | |
} | |
public static void End(string key) { | |
TimeSpan time = EndTimer(key); | |
UnityEngine.Debug.Log($"<color=#8000ff>{key} done in {time.ToString("mm':'ss':'fff")}</color>"); | |
} | |
private static TimeSpan EndTimer(string key) { | |
if (!stopwatches.ContainsKey(key)) | |
return TimeSpan.MinValue; | |
Stopwatch sw = stopwatches[key]; | |
sw.Stop(); | |
stopwatches.Remove(key); | |
return sw.Elapsed; | |
} | |
} | |
// Usage : | |
// Profiling.Start("Method"); | |
// Method(); | |
// Profiling.End("Method"); | |
// --> Method done in 00:01:347 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment