xref: /glogg/src/perfcounter.h (revision 045703da85a28b11b557ede4ab9f54834cd9b320)
1*045703daSNicolas Bonnefon /*
2*045703daSNicolas Bonnefon  * Copyright (C) 2015 Nicolas Bonnefon and other contributors
3*045703daSNicolas Bonnefon  *
4*045703daSNicolas Bonnefon  * This file is part of glogg.
5*045703daSNicolas Bonnefon  *
6*045703daSNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
7*045703daSNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
8*045703daSNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
9*045703daSNicolas Bonnefon  * (at your option) any later version.
10*045703daSNicolas Bonnefon  *
11*045703daSNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
12*045703daSNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*045703daSNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*045703daSNicolas Bonnefon  * GNU General Public License for more details.
15*045703daSNicolas Bonnefon  *
16*045703daSNicolas Bonnefon  * You should have received a copy of the GNU General Public License
17*045703daSNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18*045703daSNicolas Bonnefon  */
19*045703daSNicolas Bonnefon 
20*045703daSNicolas Bonnefon #include <chrono>
21*045703daSNicolas Bonnefon 
22*045703daSNicolas Bonnefon // This class is a counter that remembers the number of events occuring within one second
23*045703daSNicolas Bonnefon // it can be used for performance measurement (e.g. FPS)
24*045703daSNicolas Bonnefon 
25*045703daSNicolas Bonnefon class PerfCounter {
26*045703daSNicolas Bonnefon   public:
PerfCounter()27*045703daSNicolas Bonnefon     PerfCounter() {}
28*045703daSNicolas Bonnefon 
29*045703daSNicolas Bonnefon     // Count a new event, returns true if it has been counted.
30*045703daSNicolas Bonnefon     // If the function returns false, it indicates the current second is elapsed
31*045703daSNicolas Bonnefon     // and the user should read and reset the counter before re-adding the event.
addEvent()32*045703daSNicolas Bonnefon     bool addEvent() {
33*045703daSNicolas Bonnefon         using namespace std::chrono;
34*045703daSNicolas Bonnefon         if ( counter_ == 0 )
35*045703daSNicolas Bonnefon             first_event_date_ = steady_clock::now();
36*045703daSNicolas Bonnefon 
37*045703daSNicolas Bonnefon         if ( duration_cast<microseconds>(
38*045703daSNicolas Bonnefon                     steady_clock::now() - first_event_date_ ).count() < 1000000 ) {
39*045703daSNicolas Bonnefon             ++counter_;
40*045703daSNicolas Bonnefon             return true;
41*045703daSNicolas Bonnefon         }
42*045703daSNicolas Bonnefon         else {
43*045703daSNicolas Bonnefon             return false;
44*045703daSNicolas Bonnefon         }
45*045703daSNicolas Bonnefon     }
46*045703daSNicolas Bonnefon 
47*045703daSNicolas Bonnefon     // Read and reset the counter. Returns the number of events that occured in the
48*045703daSNicolas Bonnefon     // previous second.
readAndReset()49*045703daSNicolas Bonnefon     uint32_t readAndReset() {
50*045703daSNicolas Bonnefon         uint32_t value = counter_;
51*045703daSNicolas Bonnefon         counter_ = 0;
52*045703daSNicolas Bonnefon         return value;
53*045703daSNicolas Bonnefon     }
54*045703daSNicolas Bonnefon 
55*045703daSNicolas Bonnefon   private:
56*045703daSNicolas Bonnefon     uint32_t counter_ = 0;
57*045703daSNicolas Bonnefon     std::chrono::steady_clock::time_point first_event_date_;
58*045703daSNicolas Bonnefon };
59