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.

72 lines
2.1KB

  1. #pragma once
  2. #include "SqTime.h"
  3. /**
  4. * Unfinished time class. Meant to be inserted into other code (like VCV Rack).
  5. * Averages states over time.
  6. * Will printf states every now and then.
  7. * Many ways to improve this - printing from a different thread is one.
  8. */
  9. class TimeStatsCollector
  10. {
  11. public:
  12. TimeStatsCollector()
  13. {
  14. }
  15. /**
  16. * Instrumented code calls this to start a timing sample.
  17. */
  18. void start()
  19. {
  20. startTime = SqTime::seconds();
  21. }
  22. /**
  23. * Instrumented code calls this to step timing
  24. * @param numLoops is the number of "events" since start was called.
  25. */
  26. void stop(int numLoops)
  27. {
  28. const double stop = SqTime::seconds();
  29. const double delta = stop - startTime;
  30. numDataPoints += numLoops;
  31. totalTimeFrame += delta;
  32. if (delta < minTime) {
  33. minTime = delta;
  34. } else if (delta > maxTime) {
  35. maxTime = delta;
  36. }
  37. if (numDataPoints > 1000) {
  38. const double avgTime = totalTimeFrame / numDataPoints;
  39. const double srTime = (1.0 / 44100.0);
  40. const double ratio = avgTime / srTime;
  41. totalTimeGlobal += avgTime;
  42. numFramesInTotal++;
  43. double runningAvg = totalTimeGlobal / (numFramesInTotal * srTime);
  44. printf("\nsrTime=%f avtTime=%f\n", srTime, avgTime);
  45. printf("this block: %f%% min=%f max=%f globalmax=%f running avg=%f\n",
  46. ratio * 100, minTime, maxTime, globalMax, runningAvg * 100);
  47. if (globalMax < maxTime) {
  48. globalMax = maxTime;
  49. }
  50. numDataPoints=0;
  51. totalTimeFrame = 0;
  52. minTime = 1000000000;
  53. maxTime = -100000000000;
  54. }
  55. }
  56. private:
  57. int numDataPoints = 0; // number of samples in current frame.
  58. double totalTimeFrame = 0; // Time spent in the test code this frame.
  59. double startTime; // Start time if current sample
  60. double minTime = 1000000000; // shorted time observed in current frame
  61. double maxTime = -100000000000;// longest time observed in current frame.
  62. double globalMax = -100000000000;
  63. double totalTimeGlobal = 0;
  64. int numFramesInTotal = 0;
  65. };