Skip to content

Instantly share code, notes, and snippets.

@andrewzimmer906
Created September 8, 2020 21:03
Show Gist options
  • Save andrewzimmer906/57c8e16873deef09d7b06d50f65ae3f3 to your computer and use it in GitHub Desktop.
Save andrewzimmer906/57c8e16873deef09d7b06d50f65ae3f3 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
struct TimerData {
public string name;
public List<double> times;
}
// This class isn't currently considered to be threadsafe, so you know, don't be a dummy.
public class SVRTimeProfiler
{
private static List<TimerData> _measuredTimes = new List<TimerData>();
private static Stopwatch _stopWatch = new Stopwatch();
private static string _title;
private static uint _numSamples;
public static void StartTimer(string title, uint numSamples = 0) {
_stopWatch.Start();
_title = title;
_numSamples = numSamples;
}
public static void StopTimer() {
if (_numSamples == 0) {
UnityEngine.Debug.LogFormat("[{0}] elapsed time : {1} ms", _title, _stopWatch.Elapsed.TotalMilliseconds);
}
else {
TimerData data = dataForTitle(_title);
if (data.times.Count >= _numSamples) {
double total = 0;
foreach(double time in data.times) {
total += time;
}
total /= data.times.Count;
data.times.Clear();
UnityEngine.Debug.LogFormat("[{0}] avg elapsed time : {1} ms", _title, total);
}
data.times.Add(_stopWatch.Elapsed.TotalMilliseconds);
}
_stopWatch.Stop();
_stopWatch.Reset();
}
private static TimerData dataForTitle(string title) {
foreach(TimerData curData in _measuredTimes) {
if(curData.name == title) {
return curData;
}
}
TimerData data = new TimerData();
data.name = title;
data.times = new List<double>();
_measuredTimes.Add(data);
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment