Skip to content

Instantly share code, notes, and snippets.

@leegrey
Created February 13, 2017 22:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leegrey/0004e563a95aec9991d67a6ff8fbea84 to your computer and use it in GitHub Desktop.
Save leegrey/0004e563a95aec9991d67a6ff8fbea84 to your computer and use it in GitHub Desktop.
A small debug util for timing chunks of code in Unity / C#
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