ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MyTimer.h
Go to the documentation of this file.
1 
13 #include <string>
14 #include <vector>
15 // C++11 for new duration and time
16 #include <chrono>
17 #include <ratio>
18 
24 class MyTimer {
25 
26  public :
30  MyTimer();
32  MyTimer( std::string str_name );
34  virtual ~MyTimer();
36 
40  std::string GetName() {return name;}
42 
46  double GetTotalTime( std::string str_name = std::string() );
48  double GetAverageTime(std::string str_name = std::string());
50  unsigned int GetNumberCalls( std::string str_name = std::string("default_timer") );
52 
56  void Start();
61  void Stop();
67  void Reset( std::string str_name = std::string("default_timer") );
69 
74  virtual void Report();
75 
76  // order is important, should be defined before declaring private member m_timer
80  class OneTimer {
81 
82  private :
83  // define a duration with a tick of one second by default
84  // equiv duration<long,ratio<1>>
85  // std::chrono::duration<long> total_t;
86 
87  // define a duration in millisecond for total_time, need double for conversion from tpoint
88  //typedef std::chrono::duration<long, std::milli> duration_tt;
89  typedef std::chrono::duration<double, std::milli> duration_dtt;
90  // equivalent: std::chrono::duration<double, std::ratio<1,1000> > total_t;
91 
92  // time_point, see high_resolution_clock
93  //typedef std::chrono::time_point<std::chrono::steady_clock> tpoint;
94  // default tpoint
95  // std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >
96  // seems slightly better when comparing no sleep and sleep 1 millisecond
97  typedef std::chrono::time_point< std::chrono::steady_clock, std::chrono::duration<double, std::ratio<1, 1000000000> > > tpoint;
98 
108  unsigned int n_call;
110 
111  public :
117  OneTimer();
119  ~OneTimer();
121 
125  unsigned int GetNumberCalls() {return n_call;}
133  std::vector<double> GetAllTimes(); //{return std::vector<int>();}
135 
139  void Start();
141  void Stop();
143  void Reset();
145  }; // end nested class OneTimer
146 
147  private :
148 
153  std::string name;
154 
155  // Map to register different timing, to see later
156  //std::map<std::string, std::vector<OneTimer> > m_timer;
157  //
159 
160 }; //end class MyTimer
unsigned int GetNumberCalls(std::string str_name=std::string("default_timer"))
Get the number of calls (used for average)
Definition: MyTimer.cpp:78
tpoint stop_t
time_point, stopping time
Definition: MyTimer.h:104
OneTimer()
default constructor.
Definition: MyTimer.cpp:110
void Reset(std::string str_name=std::string("default_timer"))
Reset the timer.
Definition: MyTimer.cpp:58
unsigned int n_call
number of calls to stop/start
Definition: MyTimer.h:108
std::vector< double > GetAllTimes()
Get all computed times.
Definition: MyTimer.cpp:240
void Stop()
Stop the timer.
Definition: MyTimer.cpp:50
tpoint start_t
time_point, starting time
Definition: MyTimer.h:102
MyTimer()
default constructor
Definition: MyTimer.cpp:19
std::string name
first implementation, only one name,
Definition: MyTimer.h:153
double GetTotalTime(std::string str_name=std::string())
Get the total time, anonymous or named.
Definition: MyTimer.cpp:66
MyTimer can register call(s) and print reports.
Definition: MyTimer.h:24
void Stop()
Stop the timer.
Definition: MyTimer.cpp:174
unsigned int GetNumberCalls()
Get the number of calls.
Definition: MyTimer.h:125
~OneTimer()
destructor
Definition: MyTimer.cpp:131
std::string GetName()
Get the timer name.
Definition: MyTimer.h:40
void Start()
Start the timer.
Definition: MyTimer.cpp:42
virtual ~MyTimer()
virtual destructor, class could be derived
Definition: MyTimer.cpp:36
std::chrono::duration< double, std::milli > duration_dtt
Definition: MyTimer.h:89
duration_dtt total_t
total time, a duration
Definition: MyTimer.h:106
OneTimer one_timer
Definition: MyTimer.h:158
nested class of MyTimer, contains the timers functions
Definition: MyTimer.h:80
std::chrono::time_point< std::chrono::steady_clock, std::chrono::duration< double, std::ratio< 1, 1000000000 > > > tpoint
Definition: MyTimer.h:97
void Reset()
reset the timer
Definition: MyTimer.cpp:230
void Start()
Start the timer.
Definition: MyTimer.cpp:139
double GetAverageTime(std::string str_name=std::string())
Get the average time, total time / number of calls.
Definition: MyTimer.cpp:72
virtual void Report()
Make a report for the timers.
Definition: MyTimer.cpp:84