00001 #ifndef _UT_StopWatch_h_
00002 #define _UT_StopWatch_h_
00003
00004 #include "UT_API.h"
00005 #include <time.h>
00006 #include <stdlib.h>
00007 #include <SYS/SYS_Time.h>
00008 #include <SYS/SYS_Types.h>
00009
00010
00011
00012
00013 #if defined(CLOCK_REALTIME) && (defined(LINUX) || defined(SOLARIS) || defined(MBSD))
00014 #undef CLOCK_REALTIME
00015 #endif
00016
00017 typedef
00018
00019 #if defined(CLOCK_REALTIME)
00020 timespec
00021 #else
00022 SYS_TimeVal
00023 #endif
00024
00025 UT_TIMERVAL;
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 class UT_API UT_StopWatch
00039 {
00040 public:
00041 UT_StopWatch();
00042
00043 void clear();
00044 void start();
00045 fpreal64 lap() const;
00046 fpreal64 stop();
00047 void resume();
00048
00049 fpreal64 getTime() const;
00050 bool isRunning() const;
00051
00052 fpreal64 operator-( const UT_StopWatch & );
00053
00054
00055 static fpreal64 _timeDiff(const UT_TIMERVAL &later, const UT_TIMERVAL &sooner);
00056
00057 protected:
00058 bool _running;
00059 fpreal64 _elapsedTime;
00060
00061 UT_TIMERVAL _startTime;
00062 };
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 class UT_API UT_Timer
00083 {
00084 public:
00085 UT_Timer();
00086 explicit UT_Timer(const char *block_name);
00087
00088 ~UT_Timer();
00089
00090 static void indent();
00091
00092 static void timeStamp(const char *msg1, const char *msg2=0);
00093
00094 void clear();
00095
00096 void begin( const char *block_name = NULL );
00097
00098 void end();
00099
00100 fpreal64 getBlockDuration();
00101 fpreal64 getTotalDuration();
00102 fpreal64 getAverageDuration();
00103
00104 void setDisplay( bool d ) { myDisplay = d; }
00105
00106 void displayBlockDuration( const char *block_name = NULL );
00107 void displayTotalDuration( const char *block_name = NULL );
00108 void displayAverageDuration( const char *block_name = NULL );
00109
00110 static UT_Timer *getTimer(int id);
00111
00112 private:
00113 bool myBegun;
00114 bool myDisplay;
00115 UT_StopWatch myBlockTimer;
00116 UT_StopWatch mySavedBlockTimer;
00117 fpreal64 myTotal;
00118 int myCount;
00119
00120 static int theIndentation;
00121 static UT_StopWatch theLastBlockTimer;
00122 };
00123
00124 class UT_API UT_TimerAuto
00125 {
00126 public:
00127 UT_TimerAuto(UT_Timer &ts, const char *name): myTimeStamp( ts )
00128 {
00129 myTimeStamp.begin( name );
00130 }
00131
00132 UT_TimerAuto(UT_Timer &ts): myTimeStamp( ts )
00133 {
00134 myTimeStamp.begin();
00135 }
00136
00137 ~UT_TimerAuto()
00138 {
00139 myTimeStamp.end();
00140 }
00141
00142 UT_Timer &myTimeStamp;
00143 };
00144
00145 #endif // _UT_StopWatch_h_