jack06215

[tools] fps measure

Jul 8th, 2020 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #ifndef __FRAME_PER_SECOND_STATS_HPP__
  2. #define __FRAME_PER_SECOND_STATS_HPP__
  3.  
  4.  
  5. #include <chrono>
  6. #include <string>  // fps_stats::header
  7. #include <cstdio>  // printf
  8.  
  9.  
  10. /*
  11.   Description:
  12.     Measure the statistics of the frame rate.  For every msec_ interval, the stats line will
  13.     be printed out with header_, which are passed through the constructor.
  14. */
  15. class fps_stats
  16. {
  17.     using hireso_clock = std::chrono::high_resolution_clock;
  18.  
  19.     std::chrono::milliseconds const _fps_update_interval;
  20.     std::string const _header;
  21.  
  22.     int   _frame_count;
  23.     float _fps_last;    // The fps of the last tick
  24.     float _fps_ave; // The average fps of the last interval
  25.     float _fps_ema; // The exponential moving average of fps
  26.  
  27.     std::chrono::time_point<hireso_clock, hireso_clock::duration> _t_interval_start, _t_last, _t_current;
  28.  
  29. public:
  30.     fps_stats(const std::string header_, bool start_now_ = true, int msec_ = 5000)
  31.         : _header(header_), _fps_update_interval(msec_)
  32.     {
  33.         if (start_now_)
  34.             start();
  35.     }
  36.  
  37.     void start(void)
  38.     {
  39.         _frame_count = 0;
  40.         _fps_last = _fps_ave = _fps_ema = 0;
  41.         _t_interval_start = _t_last = _t_current = hireso_clock::now();
  42.     }
  43.  
  44.     void tick(bool verbose = true)
  45.     {
  46.         using namespace std::chrono;
  47.  
  48.         _t_current = hireso_clock::now();   // the first thing to do
  49.  
  50.         auto delta_last = _t_current - _t_last;
  51.         auto delta_interval = _t_current - _t_interval_start;
  52.  
  53.         _fps_last = 1000000.0f / duration_cast<microseconds>(delta_last).count();
  54.         _t_last = _t_current;
  55.  
  56.         _frame_count++;
  57.  
  58.         if (_fps_update_interval <= delta_interval)
  59.         {
  60.             // Calculate FPS of this interval and its moving average
  61.             _fps_ave = _frame_count * 1000000.0f / duration_cast<microseconds>(delta_interval).count();
  62.             _fps_ema = 0.8f * _fps_ema + 0.2f * _fps_ave;
  63.  
  64.             // Print the statistics
  65.             if (verbose)
  66.                 printf("%s: Last %0.3fms %0.3ffps | Int %ldms %dfr %0.3ffps | EMA %0.3ffps\n",
  67.                         _header.c_str(),
  68.                         0.001f * duration_cast<microseconds>(delta_last).count(),
  69.                         _fps_last,
  70.                         duration_cast<milliseconds>(delta_interval).count(),
  71.                         _frame_count,
  72.                         _fps_ave,
  73.                         _fps_ema);
  74.  
  75.             _frame_count = 0;
  76.             _t_interval_start = _t_current;
  77.         }
  78.     }
  79.  
  80.     float last_fps(void) { return _fps_last;    }
  81.     float interval_fps(void) { return _fps_ave; }
  82.     float ema_fps(void) { return _fps_ema; }
  83. };
  84.  
  85. #endif // !__FRAME_PER_SECOND_STATS_HPP__
  86.  
  87.  
  88.  
  89. /* fps meter
  90.     fps_stats fps{ "AKAZE2" };
  91. for (int i = 1; i < max_frames; i++, fps.tick())
  92.     {  
  93. }
  94. */
Add Comment
Please, Sign In to add comment