HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_StopWatch.h
Go to the documentation of this file.
1 #ifndef _UT_StopWatch_h_
2 #define _UT_StopWatch_h_
3 
4 #include "UT_API.h"
5 #include <SYS/SYS_Deprecated.h>
6 #include <SYS/SYS_Time.h>
7 #include <SYS/SYS_Types.h>
8 #include <stdlib.h>
9 
10 /// A simple stopwatch class.
11 ///
12 /// It implements functions much like a regular stopwatch:
13 /// start(): resets stopwatch to zero, and starts it.
14 /// lap(): returns the elapsed time.
15 /// stop(): stop the stopwatch
16 /// resume(): resumes the stopwatch
17 /// getTime(): returns elapsed time, may be used when stopped.
18 /// operator-: difference in start time between two watches
19 ///
21 {
22 public:
23  void clear();
24  void start();
25  fpreal64 lap() const;
26  fpreal64 stop();
27  void resume();
28  fpreal64 restart();
29 
30  fpreal64 getTime() const;
31  bool isRunning() const;
32 
33  fpreal64 operator-(const UT_StopWatch &other);
34  SYS_TimeVal getStartTime() const;
35 
36 protected:
37  bool myRunning = false; // is the watch is running?
39 
41 };
42 
43 // this class replaces the old timeStamp function, and can be used to more
44 // easily take timing measurements
45 //
46 // you still have the old timeStamp function that works in the same way, but
47 // also, you can use begin and end event functions which will output a duration
48 // along with the timestamps. another benefit is that it becomes easy to see
49 // the contained duration of a function.
50 //
51 // you can additionally use the indent function to give you the right
52 // indentation when inserting your own printfs
53 //
54 //
55 // so, to add timestamps, do this:
56 // UT_Timer ts( "block 1" );
57 // ts.end(); // no need if scope ends, otherwise it displays
58 //
59 // don't mismatch your begins and ends or else everything will break
60 // if you want running totals send in the pointer to total
62 {
63 public:
64  UT_Timer();
65  explicit UT_Timer(const char *block_name);
66 
67  ~UT_Timer();
68 
69  UT_Timer(const UT_Timer &) = delete;
70  UT_Timer &operator=(const UT_Timer &) = delete;
71 
72  static void indent();
73 
74  static void timeStamp(const char *msg1, const char *msg2=0);
75 
76  void clear();
77 
78  void begin( const char *block_name = NULL );
79  int lap() { myCount++; return myCount; }
80  void end();
81 
82  bool isRunning() const { return myBegun; }
83 
84  fpreal64 getBlockDuration();
85  fpreal64 getTotalDuration();
86  fpreal64 getAverageDuration();
87 
88  void setDisplay( bool d ) { myDisplay = d; }
89  bool willDisplay() const { return myDisplay; }
90 
91  void displayBlockDuration( const char *block_name = NULL );
92  void displayTotalDuration( const char *block_name = NULL );
93  void displayAverageDuration( const char *block_name = NULL );
94 
95  static UT_Timer *getTimer(int id);
96 
97 private:
98  bool myBegun;
99  bool myDisplay;
100  UT_StopWatch myBlockTimer;
101  UT_StopWatch mySavedBlockTimer;
102  fpreal64 myTotal;
103  int myCount;
104 
105  static int theIndentation;
106  static UT_StopWatch theLastBlockTimer;
107 };
108 
109 // For use with UT_TimerAuto to keep track of total time
111 {
112 public:
113  UT_TimerDisplayTotal(const char* block_name)
114  : myBlockName(block_name)
115  {
116  }
118  {
119  displayAverageDuration(myBlockName);
120  displayTotalDuration(myBlockName);
121  }
122 private:
123  const char* myBlockName;
124 };
125 
126 // Used to accumulate time into a UT_Timer/UT_TimerDisplayTotal
128 {
129 public:
130  UT_TimerAuto(UT_Timer &ts, const char *name): myTimeStamp( ts )
131  {
132  myTimeStamp.begin( name );
133  }
134 
135  UT_TimerAuto(UT_Timer &ts): myTimeStamp( ts )
136  {
137  myTimeStamp.begin();
138  }
139 
141  {
142  myTimeStamp.end();
143  }
144 
146 };
147 
149 
150 /////////////////////////////////////////////////////////////////////////////
151 //
152 // Inline implementations
153 //
154 
155 inline void
156 UT_StopWatch::clear()
157 {
158  myElapsedTime = 0;
159 }
160 
161 inline void
163 {
164  clear();
165  resume();
166 }
167 
168 inline fpreal64
170 {
171  if (myRunning)
172  {
173  SYS_TimeVal now;
174 
175  SYSgettimeofday(&now);
176  return myElapsedTime + SYStimeDiff(now, myStartTime);
177  }
178  return myElapsedTime;
179 }
180 
181 inline fpreal64
183 {
184  if (myRunning)
185  {
186  SYS_TimeVal now;
187 
188  SYSgettimeofday(&now);
189  myRunning = false;
190 
191  return myElapsedTime += SYStimeDiff(now, myStartTime);
192  }
193  return myElapsedTime;
194 }
195 
196 inline void
198 {
199  if (myRunning)
200  clear();
201 
203  myRunning = true;
204 }
205 
206 inline fpreal64
208 {
209  fpreal64 now = stop();
210  start();
211  return now;
212 }
213 
214 inline fpreal64
216 {
217  return lap(); // should they be different?
218 }
219 
220 inline bool
222 {
223  return myRunning;
224 }
225 
226 inline fpreal64
228 {
229  return SYStimeDiff(myStartTime, other.myStartTime);
230 }
231 
232 inline SYS_TimeVal
234 {
235  return myStartTime;
236 }
237 
238 #endif // _UT_StopWatch_h_
struct timeval SYS_TimeVal
Definition: SYS_Time.h:31
fpreal64 restart()
Definition: UT_StopWatch.h:207
fpreal64 SYStimeDiff(const SYS_TimeVal &end, const SYS_TimeVal &start)
Definition: SYS_Time.h:58
fpreal64 lap() const
Definition: UT_StopWatch.h:169
UT_TimerDisplayTotal(const char *block_name)
Definition: UT_StopWatch.h:113
int lap()
Definition: UT_StopWatch.h:79
fpreal64 getTime() const
Definition: UT_StopWatch.h:215
SYS_API int SYSgettimeofday(SYS_TimeVal *tval)
SYS_TimeVal myStartTime
Definition: UT_StopWatch.h:40
#define UT_API
Definition: UT_API.h:14
SYS_TimeVal getStartTime() const
Definition: UT_StopWatch.h:233
UT_TimerAuto(UT_Timer &ts)
Definition: UT_StopWatch.h:135
double fpreal64
Definition: SYS_Types.h:201
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
GLuint GLuint end
Definition: glcorearb.h:475
UT_TimerAuto(UT_Timer &ts, const char *name)
Definition: UT_StopWatch.h:130
GLuint const GLchar * name
Definition: glcorearb.h:786
SYS_TimeVal UT_TIMERVAL
Definition: UT_StopWatch.h:148
void displayAverageDuration(const char *block_name=NULL)
bool isRunning() const
Definition: UT_StopWatch.h:82
void displayTotalDuration(const char *block_name=NULL)
UT_Timer & myTimeStamp
Definition: UT_StopWatch.h:145
LeafData & operator=(const LeafData &)=delete
fpreal64 stop()
Definition: UT_StopWatch.h:182
bool isRunning() const
Definition: UT_StopWatch.h:221
fpreal64 myElapsedTime
Definition: UT_StopWatch.h:38
fpreal64 operator-(const UT_StopWatch &other)
Definition: UT_StopWatch.h:227
void setDisplay(bool d)
Definition: UT_StopWatch.h:88
bool willDisplay() const
Definition: UT_StopWatch.h:89
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:566