2020-02-03 22:48:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-03-09 16:22:33 +00:00
|
|
|
|
public class Timer {
|
2020-02-03 22:48:56 +00:00
|
|
|
|
|
|
|
|
|
private readonly Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
private readonly double targetTime;
|
|
|
|
|
private readonly string name;
|
|
|
|
|
private double startTime = 0.0;
|
2020-02-03 23:39:24 +00:00
|
|
|
|
private int[] histogram = new int[21];
|
2020-02-03 22:48:56 +00:00
|
|
|
|
private int totalFrames = 0;
|
|
|
|
|
|
|
|
|
|
public Timer(double targetTime, string name) {
|
|
|
|
|
this.targetTime = targetTime;
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start() {
|
|
|
|
|
startTime = stopwatch.Elapsed.TotalSeconds;
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop() {
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
totalFrames++;
|
|
|
|
|
double frameTime = stopwatch.Elapsed.TotalSeconds - startTime;
|
2020-02-03 23:39:24 +00:00
|
|
|
|
int bucket = FMath.Clamp((int) (10.0f * frameTime / targetTime), 0, 20);
|
2020-02-03 22:48:56 +00:00
|
|
|
|
histogram[bucket]++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DumpStats() {
|
|
|
|
|
Debug.WriteLine(name + ".DumpStats():");
|
2020-02-03 23:39:24 +00:00
|
|
|
|
for (int i = 0; i < histogram.Length; i++) {
|
2020-02-03 22:48:56 +00:00
|
|
|
|
int value = histogram[i];
|
2020-02-03 23:39:24 +00:00
|
|
|
|
if (value == 0) {
|
|
|
|
|
continue;
|
2020-02-03 22:48:56 +00:00
|
|
|
|
}
|
2020-02-03 23:39:24 +00:00
|
|
|
|
// Every star is one percent.
|
|
|
|
|
int numStars = FMath.Clamp(100 * value / totalFrames, 1, 100);
|
2020-02-04 14:54:21 +00:00
|
|
|
|
string prefix;
|
2020-02-03 23:39:24 +00:00
|
|
|
|
if (i == histogram.Length - 1) {
|
2020-02-04 00:01:45 +00:00
|
|
|
|
prefix = " 200+%: ";
|
2020-02-04 14:54:21 +00:00
|
|
|
|
} else {
|
|
|
|
|
prefix = String.Format("{0,3}-{1,3}%: ", i * 10, (i + 1) * 10);
|
2020-02-03 23:39:24 +00:00
|
|
|
|
}
|
|
|
|
|
string stars = new string('*', numStars);
|
|
|
|
|
Debug.WriteLine(String.Format("{0}{1,-100} {2}", prefix, stars, value));
|
2020-02-03 22:48:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|