Choreonoid  1.8
TimeMeasure.h
Go to the documentation of this file.
1 
2 #ifndef CNOID_UTIL_TIME_MEASURE_H
3 #define CNOID_UTIL_TIME_MEASURE_H
4 
5 #ifndef _WIN32
6 
7 #include <time.h>
8 #ifdef _POSIX_C_SOURCE
9 #if _POSIX_C_SOURCE >= 199309L
10 #define USE_GETTIME
11 #endif
12 #endif
13 
14 namespace cnoid {
15 
17 {
18 #ifdef USE_GETTIME
19  struct timespec tp;
20 #else
21  struct timeval tv;
22 #endif
23  double time_;
24  double totalTime_;
25  int numCalls;
26 
27 public:
28  inline TimeMeasure() {
29  totalTime_ = 0.0;
30  numCalls = 0;
31  }
32 
33  inline void begin() {
34 #ifdef USE_GETTIME
35  clock_gettime(CLOCK_MONOTONIC, &tp) ;
36 #else
37  gettimeofday(&tv, 0);
38 #endif
39  }
40 
41  inline void end(){
42 #ifdef USE_GETTIME
43  double beginTime = tp.tv_sec + (double)tp.tv_nsec * 1.0e-9;
44  clock_gettime(CLOCK_MONOTONIC, &tp);
45  double endTime = tp.tv_sec + (double)tp.tv_nsec * 1.0e-9;
46 #else
47  double beginTime = tv.tv_sec + (double)tv.tv_usec * 1.0e-6;
48  gettimeofday(&tv, 0);
49  double endTime = tv.tv_sec + (double)tv.tv_usec * 1.0e-6;
50 #endif
51  time_ = endTime - beginTime;
52  totalTime_ += time_;
53  numCalls++;
54  }
55 
56  inline double measure() {
57  end();
58  return time_;
59  }
60 
61  inline double time() { return time_; }
62  inline double totalTime() { return totalTime_; }
63  inline double avarageTime() { return totalTime_ / numCalls; }
64 
65 };
66 }
67 
68 #else
69 #include <windows.h>
70 
71 namespace cnoid {
72 
73 typedef unsigned __int64 ulonglong;
74 
75 class TimeMeasure
76 {
77  ulonglong iTimerScale;
78  ulonglong beginTime;
79  ulonglong endTime;
80  double time_;
81  double totalTime_;
82  int numCalls;
83 
84 public:
85  inline TimeMeasure() {
86  totalTime_ = 0.0;
87  numCalls = 0;
88  BOOL iDummyBool = QueryPerformanceFrequency ((LARGE_INTEGER *) &iTimerScale);
89  if(!iDummyBool)
90  iTimerScale=1;
91  }
92 
93  inline void begin() {
94  BOOL iDummyBool = QueryPerformanceCounter ((LARGE_INTEGER *) &beginTime);
95  if(!iDummyBool)
96  beginTime=1;
97  }
98 
99  inline void end(){
100  BOOL iDummyBool = QueryPerformanceCounter ((LARGE_INTEGER *) &endTime);
101  if(!iDummyBool)
102  endTime=0;
103  time_ = (double)(endTime - beginTime) / iTimerScale;
104  totalTime_ += time_;
105  numCalls++;
106  }
107  inline double measure() {
108  end();
109  return time_;
110  }
111  inline double time() { return time_; }
112  inline double totalTime() { return totalTime_; }
113  inline double avarageTime() { return totalTime_ / numCalls; }
114 };
115 
116 }
117 
118 #endif
119 
120 #endif
cnoid::TimeMeasure::measure
double measure()
Definition: TimeMeasure.h:56
cnoid::TimeMeasure::avarageTime
double avarageTime()
Definition: TimeMeasure.h:63
cnoid::TimeMeasure
Definition: TimeMeasure.h:16
cnoid::TimeMeasure::time
double time()
Definition: TimeMeasure.h:61
cnoid::TimeMeasure::totalTime
double totalTime()
Definition: TimeMeasure.h:62
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::TimeMeasure::begin
void begin()
Definition: TimeMeasure.h:33
cnoid::TimeMeasure::end
void end()
Definition: TimeMeasure.h:41
cnoid::TimeMeasure::TimeMeasure
TimeMeasure()
Definition: TimeMeasure.h:28