Created
February 13, 2017 22:54
-
-
Save leegrey/0004e563a95aec9991d67a6ff8fbea84 to your computer and use it in GitHub Desktop.
A small debug util for timing chunks of code in Unity / C#
This file contains 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.Collections.Generic; | |
using System.Diagnostics; | |
using Debug = UnityEngine.Debug; | |
/* | |
USAGE: | |
ProcessTimer.Start("unique_process_id"); | |
// do stuff | |
ProcessTimer.End("unique_process_id", "An optional additional message to display with results"); | |
*/ | |
namespace LG | |
{ | |
public static class ProcessTimer | |
{ | |
private static Dictionary<string, Stopwatch> timers = new Dictionary<string, Stopwatch>(); | |
public static void Start(string id) | |
{ | |
var stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
timers[id] = stopwatch; | |
} | |
public static void End(string id, string message = null) | |
{ | |
Stopwatch stopwatch; | |
bool hasStopwatch = timers.TryGetValue(id, out stopwatch); | |
if (!hasStopwatch) return; | |
stopwatch.Stop(); | |
if (message == null) message = ""; | |
Debug.Log( | |
id + ": " + message + ": "+ stopwatch.ElapsedMilliseconds + "ms / " | |
+ stopwatch.ElapsedTicks + " ticks"); | |
timers[id] = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment