-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfps_stats.hpp
85 lines (66 loc) · 2.62 KB
/
fps_stats.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef __FRAME_PER_SECOND_STATS_HPP__
#define __FRAME_PER_SECOND_STATS_HPP__
#include <chrono>
#include <string> // fps_stats::header
#include <cstdio> // printf
/*
Description:
Measure the statistics of the frame rate. For every msec_ interval, the stats line will
be printed out with header_, which are passed through the constructor.
*/
class fps_stats
{
using hireso_clock = std::chrono::high_resolution_clock;
std::chrono::milliseconds const _fps_update_interval;
std::string const _header;
int _frame_count;
float _fps_last; // The fps of the last tick
float _fps_ave; // The average fps of the last interval
float _fps_ema; // The exponential moving average of fps
std::chrono::time_point<hireso_clock, hireso_clock::duration> _t_interval_start, _t_last, _t_current;
public:
fps_stats(const std::string header_, bool start_now_ = true, int msec_ = 5000)
: _header(header_), _fps_update_interval(msec_)
{
if (start_now_)
start();
}
void start(void)
{
_frame_count = 0;
_fps_last = _fps_ave = _fps_ema = 0;
_t_interval_start = _t_last = _t_current = hireso_clock::now();
}
void tick(bool verbose = true)
{
using namespace std::chrono;
_t_current = hireso_clock::now(); // the first thing to do
auto delta_last = _t_current - _t_last;
auto delta_interval = _t_current - _t_interval_start;
_fps_last = 1000000.0f / duration_cast<microseconds>(delta_last).count();
_t_last = _t_current;
_frame_count++;
if (_fps_update_interval <= delta_interval)
{
// Calculate FPS of this interval and its moving average
_fps_ave = _frame_count * 1000000.0f / duration_cast<microseconds>(delta_interval).count();
_fps_ema = 0.8f * _fps_ema + 0.2f * _fps_ave;
// Print the statistics
if (verbose)
printf("%s: Last %0.3fms %0.3ffps | Int %ldms %dfr %0.3ffps | EMA %0.3ffps\n",
_header.c_str(),
0.001f * duration_cast<microseconds>(delta_last).count(),
_fps_last,
duration_cast<milliseconds>(delta_interval).count(),
_frame_count,
_fps_ave,
_fps_ema);
_frame_count = 0;
_t_interval_start = _t_current;
}
}
float last_fps(void) { return _fps_last; }
float interval_fps(void) { return _fps_ave; }
float ema_fps(void) { return _fps_ema; }
};
#endif // !__FRAME_PER_SECOND_STATS_HPP__