ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MyTimer.cpp
Go to the documentation of this file.
1 
8 #include "MyTimer.h"
9 
10 #include <iostream>
11 #include <cassert>
12 // time manipulation
13 #include <iomanip>
14 #include <ctime>
15 
16 
17 //using namespace std::chrono;
18 
19 MyTimer::MyTimer() : name("default_timer") {
20 #ifdef DEBUG_UTIL
21  std::cout << "Constructor default MyTimer "<< std::endl;
22 #endif
23 
24  one_timer = OneTimer();
25 }
26 
27 MyTimer::MyTimer( std::string str_name ) {
28 #ifdef DEBUG_UTIL
29  std::cout << "Constructor default MyTimer "<< std::endl;
30 #endif
31 
32  name = str_name;
33  one_timer = OneTimer();
34 }
35 
37 #ifdef DEBUG_UTIL
38  std::cout << "Destructor MyTimer "<< std::endl;
39 #endif
40 }
41 
43 #ifdef DEBUG_UTIL
44  std::cout << "MyTimer::Start " << std::endl;
45 #endif
46 
47  one_timer.Start();
48 }
49 
50 void MyTimer::Stop() {
51 #ifdef DEBUG_UTIL
52  std::cout << "MyTimer::Stop " << std::endl;
53 #endif
54 
55  one_timer.Stop();
56 }
57 
58 void MyTimer::Reset( std::string str_name ) { //= std::string("default_timer") ) {
59 #ifdef DEBUG_UTIL
60  std::cout << "MyTimer::Reset " << str_name << std::endl;
61 #endif
62 
63  one_timer.Reset();
64 }
65 
66 double MyTimer::GetTotalTime( std::string str_name )
67 {
68  std::vector<double> times = one_timer.GetAllTimes();
69  return times[0];
70 }
71 
72 double MyTimer::GetAverageTime( std::string str_name )
73 {
74  std::vector<double> times = one_timer.GetAllTimes();
75  return times[1];
76 }
77 
78 unsigned int MyTimer::GetNumberCalls( std::string str_name ) {
79 
80  return one_timer.GetNumberCalls();
81 }
82 
83 
85 #ifdef DEBUG_UTIL
86  std::cout << "MyTimer::Report " << std::endl;
87 #endif
88 
89  std::cout << "====================" << std::endl;
90  std::cout << "MyTimer Report, all times in seconds" << std::endl;
91 
92  std::cout << "\n Name : " << name << std::endl;
93  std::cout << " Called " << one_timer.GetNumberCalls() << " times\n " <<std::endl;
94  std::vector<double> times = one_timer.GetAllTimes();
95 
96  assert ( times.size() == 4);
97 
98  // format outpu
99  std::cout.precision(6);
100  std::cout << std::setw(15) << "Total time" << std::setw(15) << "Average" << std::setw(15) << "Minimum" << std::setw(15) << "Maximum" << std::endl;
101  std::cout << std::setw(15) << times[0] << std::setw(15) << std::scientific << times[1]
102  << std::setw(15) << std::scientific << times[2] << std::setw(15) << times[3] << std::endl;
103 
104 
105  std::cout << "\n====================" << std::endl;
106 }
107 
109 
111 #ifdef DEBUG_UTIL
112  std::cout << "Constructor default OneTimer "<< std::endl;
113 #endif
114 
115  // not necessary for time_point, count set to 0
116  start_t = tpoint();
117  stop_t = tpoint();
118 
119  // set an initial value of 0 ticks
120  total_t = duration_dtt(0);
121  // need extension to have multiple times
122  n_call =0;
123 
124  // assertion, total_t set explicitly to 0
125  assert ( total_t.count() == 0 );
126  // default constructor set to 0
127  assert ( start_t.time_since_epoch().count() == 0 );
128  assert ( stop_t.time_since_epoch().count() == 0 );
129 }
130 
132 #ifdef DEBUG_UTIL
133  std::cout << "Destructor OneTimer "<< std::endl;
134 #endif
135  // all on stack
136 
137 }
138 
140 #ifdef DEBUG_UTIL
141  std::cout << "OneTimer::Start " << std::endl;
142 #endif
143 
144  // Check correct order of Start() and Stop()
145  //assert ( start_t.time_since_epoch().count() == 0 );
146  if ( start_t.time_since_epoch().count() != 0 ) {
147  std::cout << "Warning in MyTimer::Start(), maybe called 2 times " << std::endl;
148  std::cout << "Warning The starting time will not modified " << std::endl;
149  }
150 
151  // define tme_point
152  //std::chrono::system_clock::time_point sys_tp_now = std::chrono::system_clock::now();
153  // transform to c style
154  //std::time_t now_c = std::chrono::system_clock::to_time_t(sys_tp_now);
155  // Convert to struct tm, C style time
156  //tm* t = std::localtime(&now_c);
157  // Write the time to the console, BUG GCC even 4.8 4.9 ? std::put_time not available
158  // should use system call, strftime
159  //std::cout << std::put_time(t, "%H:%M:%S"); // << std::endl;
160  //char tmp_char[25];
161  //strftime(tmp_char,25,"%Y-%m-%d %H:%M:%S",t);
162  //std::cout << "tmp_char " << tmp_char << std::endl;
163 
164 
165  // increment n_call here or ?
166  start_t = std::chrono::steady_clock::now();
167  stop_t = tpoint();
168 #ifndef DEBUG_UTIL
169  std::cout << " start_t.time_since_epoch().count() " << start_t.time_since_epoch().count() << std::endl;
170  std::cout << " stop_t.time_since_epoch().count() " << stop_t.time_since_epoch().count() << std::endl;
171 #endif
172 }
173 
175 #ifdef DEBUG_UTIL
176  std::cout << "OneTimer::Stop " << std::endl;
177 #endif
178 
179  // Check correct order of Start() and Stop()
180  //assert ( stop_t.time_since_epoch().count() == 0 );
181  if ( stop_t.time_since_epoch().count() != 0 ) {
182  std::cout << "Warning in MyTimer::Stop(), maybe called 2 times " << std::endl;
183  std::cout << "Warning Stopping timer is discarded " << std::endl;
184  return;
185  }
186 
187  if ( start_t.time_since_epoch().count() == 0 ) {
188  std::cout << "Warning in MyTimer::Stop(), Start() has not been called " << std::endl;
189  std::cout << "Warning Stopping timer is discarded " << std::endl;
190  return;
191  }
192 
193  stop_t = std::chrono::steady_clock::now();
194  // update time interval
195  total_t += stop_t - start_t;
196  // update the number of call
197  ++n_call;
198  // could set start of 0, allow to check if order Start(), Stop() is correct
199  start_t = tpoint();
200 
201 /*
202 #ifdef DEBUG_UTIL
203  std::cout << "stop_t.time_since_epoch().count() " << stop_t.time_since_epoch().count() << std::endl;
204  std::cout << "stop_t.time_since_epoch().count() " << stop_t.time_since_epoch().count() << std::endl;
205  std::cout << "stop_t.count() -start_t.count() " << stop_t.time_since_epoch().count() - start_t.time_since_epoch().count() << std::endl;
206 #endif
207 */
208 
209  // auto, suggested is an interval, but different ration
210  // std::chrono::duration<long int, std::ratio<1l, 1000000000l> >
211  //auto tmp = stop_t - start_t;
212  //std::cout << "diff_tp.count() " << diff_tp.time_since_epoch().count() << std::endl;
213  //std::cout << tmp << std::endl;
214  //std::cout << "tmp.count() " << tmp.count() << std::endl;
215  //std::cout << "tmp in milliseconds " << duration_tt(tmp).count() << std::endl;
216  //std::cout << "tmp in milliseconds " << std::chrono::duration<double, std::milli>(tmp).count() << std::endl;
217 
218 
219  //std::cout << start_t;
220 
221  // increment n_call here or ?
222  //total_t = stop_t.time_since_epoch().count() - start_t.time_since_epoch().count();
223  //total_t( stop_t.time_since_epoch().count() - start_t.time_since_epoch().count() );
224  //total_t = stop_t.time_since_epoch() -start_t.time_since_epoch();
225  //total_t = duration_tt( stop_t -start_t );
226  //total_t = duration_tt( stop_t.time_since_epoch() -start_t.time_since_epoch() );
227  //std::cout << " total_t.count() " << total_t.count() << std::endl;
228 }
229 
231 #ifdef DEBUG_UTIL
232  std::cout << "OneTimer::Reset " << std::endl;
233 #endif
234 
235  total_t = duration_dtt(0);
236  // need extension to have multiple times
237  n_call =0;
238 }
239 
240 std::vector<double> MyTimer::OneTimer::GetAllTimes() {
241 
242  std::vector<double> all_times;
243 
244  // return time in seconds by default
245  // total time
246  //all_times.push_back ( total_t.count() );
247  all_times.push_back ( std::chrono::duration<double>(total_t).count() );
248  // average
249  all_times.push_back ( std::chrono::duration<double>(total_t).count() / n_call );
250 
251  // minimum
252  all_times.push_back ( 0. );
253  // maximum
254  all_times.push_back ( 0. );
255 
256  return all_times;
257 }
258 
259 
260 
261 
262 
263 
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
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
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
Define a timer class, to be used for testing performance.
virtual void Report()
Make a report for the timers.
Definition: MyTimer.cpp:84