A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB

  1. using System;
  2. using System.Diagnostics;
  3. namespace SemiColinGames {
  4. public class Timer {
  5. private readonly Stopwatch stopwatch = new Stopwatch();
  6. private readonly double targetTime;
  7. private readonly string name;
  8. private readonly int[] histogram = new int[21];
  9. private double startTime = 0.0;
  10. private int totalFrames = 0;
  11. public Timer(double targetTime, string name) {
  12. this.targetTime = targetTime;
  13. this.name = name;
  14. }
  15. public void Start() {
  16. startTime = stopwatch.Elapsed.TotalSeconds;
  17. stopwatch.Start();
  18. }
  19. public void Stop() {
  20. stopwatch.Stop();
  21. totalFrames++;
  22. double frameTime = stopwatch.Elapsed.TotalSeconds - startTime;
  23. int bucket = FMath.Clamp((int) (10.0f * frameTime / targetTime), 0, 20);
  24. histogram[bucket]++;
  25. }
  26. [Conditional("DEBUG")]
  27. public void DumpStats() {
  28. Debug.WriteLine(name + ".DumpStats():");
  29. for (int i = 0; i < histogram.Length; i++) {
  30. int value = histogram[i];
  31. if (value == 0) {
  32. continue;
  33. }
  34. // Every star is one percent.
  35. int numStars = FMath.Clamp(100 * value / totalFrames, 1, 100);
  36. string prefix;
  37. if (i == histogram.Length - 1) {
  38. prefix = " 200+%: ";
  39. } else {
  40. prefix = String.Format("{0,3}-{1,3}%: ", i * 10, (i + 1) * 10);
  41. }
  42. string stars = new string('*', numStars);
  43. Debug.WriteLine(String.Format("{0}{1,-100} {2}", prefix, stars, value));
  44. }
  45. }
  46. }
  47. }