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 <time.h>
6 #include <stdlib.h>
7 #include <SYS/SYS_Time.h>
8 #include <SYS/SYS_Types.h>
9 
11 
12 //
13 // a simple stopwatch class. It implements functions much like
14 // a regular stopwatch:
15 // start(): resets stopwatch to zero, and starts it.
16 // lap(): returns the elapsed time.
17 // stop(); stop the stopwatch
18 // resume(); resumes the stopwatch
19 // getTime(); returns elapsed time, may be used when stopped.
20 // operator-: difference in start time between two watches
21 //
22 
24 {
25 public:
26  UT_StopWatch();
27 
28  void clear();
29  void start();
30  fpreal64 lap() const;
31  fpreal64 stop();
32  void resume();
33  fpreal64 restart() { auto now = stop(); start(); return now; }
34 
35  fpreal64 getTime() const;
36  bool isRunning() const;
37 
38  fpreal64 operator-( const UT_StopWatch & );
39  UT_TIMERVAL getStartTime() const;
40 
41  static fpreal64 _timeDiff(const UT_TIMERVAL &later, const UT_TIMERVAL &sooner);
42 
43 protected:
44  bool _running; // the watch is running
46 
48 };
49 
50 // this class replaces the old timeStamp function, and can be used to more
51 // easily take timing measurements
52 //
53 // you still have the old timeStamp function that works in the same way, but
54 // also, you can use begin and end event functions which will output a duration
55 // along with the timestamps. another benefit is that it becomes easy to see
56 // the contained duration of a function.
57 //
58 // you can additionally use the indent function to give you the right
59 // indentation when inserting your own printfs
60 //
61 //
62 // so, to add timestamps, do this:
63 // UT_Timer ts( "block 1" );
64 // ts.end(); // no need if scope ends, otherwise it displays
65 //
66 // don't mismatch your begins and ends or else everything will break
67 // if you want running totals send in the pointer to total
69 {
70 public:
71  UT_Timer();
72  explicit UT_Timer(const char *block_name);
73 
74  ~UT_Timer();
75 
76  UT_Timer(const UT_Timer &) = delete;
77  UT_Timer &operator=(const UT_Timer &) = delete;
78 
79  static void indent();
80 
81  static void timeStamp(const char *msg1, const char *msg2=0);
82 
83  void clear();
84 
85  void begin( const char *block_name = NULL );
86  int lap() { myCount++; return myCount; }
87  void end();
88 
89  bool isRunning() const { return myBegun; }
90 
91  fpreal64 getBlockDuration();
92  fpreal64 getTotalDuration();
93  fpreal64 getAverageDuration();
94 
95  void setDisplay( bool d ) { myDisplay = d; }
96  bool willDisplay() const { return myDisplay; }
97 
98  void displayBlockDuration( const char *block_name = NULL );
99  void displayTotalDuration( const char *block_name = NULL );
100  void displayAverageDuration( const char *block_name = NULL );
101 
102  static UT_Timer *getTimer(int id);
103 
104 private:
105  bool myBegun;
106  bool myDisplay;
107  UT_StopWatch myBlockTimer;
108  UT_StopWatch mySavedBlockTimer;
109  fpreal64 myTotal;
110  int myCount;
111 
112  static int theIndentation;
113  static UT_StopWatch theLastBlockTimer;
114 };
115 
116 // For use with UT_TimerAuto to keep track of total time
118 {
119 public:
120  UT_TimerDisplayTotal(const char* block_name)
121  : myBlockName(block_name)
122  {
123  }
125  {
126  displayAverageDuration(myBlockName);
127  displayTotalDuration(myBlockName);
128  }
129 private:
130  const char* myBlockName;
131 };
132 
133 // Used to accumulate time into a UT_Timer/UT_TimerDisplayTotal
135 {
136 public:
137  UT_TimerAuto(UT_Timer &ts, const char *name): myTimeStamp( ts )
138  {
139  myTimeStamp.begin( name );
140  }
141 
142  UT_TimerAuto(UT_Timer &ts): myTimeStamp( ts )
143  {
144  myTimeStamp.begin();
145  }
146 
148  {
149  myTimeStamp.end();
150  }
151 
153 };
154 
155 #endif // _UT_StopWatch_h_
struct timeval SYS_TimeVal
Definition: SYS_Time.h:28
fpreal64 restart()
Definition: UT_StopWatch.h:33
UT_TimerDisplayTotal(const char *block_name)
Definition: UT_StopWatch.h:120
int lap()
Definition: UT_StopWatch.h:86
GLuint start
Definition: glcorearb.h:475
IMATH_HOSTDEVICE constexpr Plane3< T > operator-(const Plane3< T > &plane) IMATH_NOEXCEPT
Reflect the pla.
Definition: ImathPlane.h:253
#define UT_API
Definition: UT_API.h:14
fpreal64 _elapsedTime
Definition: UT_StopWatch.h:45
UT_TimerAuto(UT_Timer &ts)
Definition: UT_StopWatch.h:142
double fpreal64
Definition: SYS_Types.h:201
GLuint GLuint end
Definition: glcorearb.h:475
UT_TimerAuto(UT_Timer &ts, const char *name)
Definition: UT_StopWatch.h:137
UT_TIMERVAL _startTime
Definition: UT_StopWatch.h:47
GLuint const GLchar * name
Definition: glcorearb.h:786
SYS_TimeVal UT_TIMERVAL
Definition: UT_StopWatch.h:10
void displayAverageDuration(const char *block_name=NULL)
bool isRunning() const
Definition: UT_StopWatch.h:89
void displayTotalDuration(const char *block_name=NULL)
UT_Timer & myTimeStamp
Definition: UT_StopWatch.h:152
void setDisplay(bool d)
Definition: UT_StopWatch.h:95
bool willDisplay() const
Definition: UT_StopWatch.h:96
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:483