Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _PORTABLE_TIMER_HH
00023 #define _PORTABLE_TIMER_HH
00024
00025 #include <ctime>
00026 #include <cstdlib>
00027
00028 #include <time.h>
00029
00030
00031 #define USEC_IN_SEC 1000000
00032
00033
00034
00035
00036
00037 #ifdef _MSC_VER
00038
00039 #define NOMINMAX
00040 #include <windows.h>
00041
00042
00043
00044
00045
00046
00047 class Portable_Timer
00048 {
00049 public:
00050
00051 typedef struct {
00052 LARGE_INTEGER start;
00053 LARGE_INTEGER stop;
00054 } stopWatch;
00055
00056
00057 Portable_Timer()
00058 {
00059 startVal.QuadPart = 0;
00060 stopVal.QuadPart = 0;
00061 QueryPerformanceFrequency(&frequency);
00062 }
00063
00064 void start() { QueryPerformanceCounter(&startVal); }
00065
00066 void stop() { QueryPerformanceCounter(&stopVal); }
00067
00068 double elapsed() {
00069 LARGE_INTEGER time;
00070 time.QuadPart = stopVal.QuadPart - startVal.QuadPart;
00071 return LIToSecs(time);
00072 }
00073
00074 double user_time() { return elapsed(); }
00075
00076
00077 private:
00078
00079 double LIToSecs(LARGE_INTEGER& L) {
00080 return ((double)L.QuadPart /(double)frequency.QuadPart) ;
00081 }
00082
00083 LARGE_INTEGER startVal;
00084 LARGE_INTEGER stopVal;
00085 LARGE_INTEGER frequency;
00086
00087
00088 };
00089
00090 #else
00091
00092 #include <sys/time.h>
00093 #include <sys/resource.h>
00094 #include <unistd.h>
00095 #include <sys/times.h>
00096
00097 class Portable_Timer
00098 {
00099 public:
00100
00101 Portable_Timer()
00102 {
00103 m_clkid = BtlConfig::Instance.realclock ? CLOCK_REALTIME : CLOCK_PROCESS_CPUTIME_ID;
00104 }
00105
00106 Portable_Timer(int clkid) : m_clkid(clkid)
00107 {}
00108
00109 void start()
00110 {
00111 timespec ts;
00112 clock_gettime(m_clkid, &ts);
00113 m_start_time = double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
00114
00115 }
00116
00117 void stop()
00118 {
00119 timespec ts;
00120 clock_gettime(m_clkid, &ts);
00121 m_stop_time = double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
00122
00123 }
00124
00125 double elapsed()
00126 {
00127 return user_time();
00128 }
00129
00130 double user_time()
00131 {
00132 return m_stop_time - m_start_time;
00133 }
00134
00135
00136 private:
00137
00138 int m_clkid;
00139 double m_stop_time, m_start_time;
00140
00141 };
00142
00143 #endif
00144
00145 #endif // PORTABLE_TIMER_HPP