Collision Checker
performance_timers.cc
Go to the documentation of this file.
2 #include <stdint.h>
3 #include <cstdlib>
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 
8 namespace test {
9 
10 #if (TIME_PROFILE_ENABLED)
11 constexpr int TIMER_COUNT = 20;
12 
13 std::vector<unsigned long> perfdata_0;
14 unsigned long perf_data[TIMER_COUNT];
15 uint64_t call_count[TIMER_COUNT];
16 Timer perf_timers[TIMER_COUNT];
17 std::string timer_messages[TIMER_COUNT];
18 bool timers_started[TIMER_COUNT];
19 bool timers_enabled = false;
20 #endif
21 
22 void start_timer(int index) {
23 #if (TIME_PROFILE_ENABLED)
24  if (timers_enabled) {
25  // if(timers_started[index])
26  //{
27  // perf_timers[index].resume();
28  //}
29  // else
30  //{
31  // timers_started[index]=true;
32  perf_timers[index].start();
33 
34  //}
35  }
36 #endif
37 }
38 void stop_timer(int index) {
39 #if (TIME_PROFILE_ENABLED)
40  if (timers_enabled) {
41  perf_timers[index].stop();
42  perf_data[index] += perf_timers[index].getElapsedTimeInNanoSec();
43  call_count[index]++;
44  }
45 #endif
46 }
47 
49 #if (TIME_PROFILE_ENABLED)
50 
51  timer_messages[0] = "windowQuery elapsed time: ";
52  timer_messages[1] = "timeSlice elapsed time: ";
53  timer_messages[2] = "collide elapsed time: ";
54  timer_messages[3] = "collide_3 elapsed time: ";
55  timer_messages[4] = "union elapsed time: ";
56  timer_messages[5] = "chull elapsed time: ";
57  timer_messages[6] = "TIMER_boost_obb_obb_convex_hull: ";
58  timer_messages[7] = "TIMER_get_cand: ";
59  timer_messages[8] = "TIMER_difference :";
60  timer_messages[9] = "GRID_BUILD :";
61  timer_messages[10] = "GRID_CANDIDATES :";
62  timer_messages[11] = "GRID_NARROWPHASE :";
63  timer_messages[12] = "GRID_STATIC_TOTAL :";
64  timer_messages[13] = "GRID_HASHING :";
65  timer_messages[14] = "grid_total_body :";
66  timer_messages[15] = "TIMER_poly_build :";
67  timer_messages[16] = "TIMER_grid_window_query :";
68  timer_messages[17] = "TIMER_polygon_enclosure :";
69 
70  for (int i = 0; i < TIMER_COUNT; i++) {
71  perf_data[i] = 0;
72  call_count[i] = 0;
73  }
74  timers_enabled = true;
75 #else
76  volatile int a = 0;
77  asm volatile("nop");
78 #endif
79 
80  return 0;
81 }
82 
84 #if (TIME_PROFILE_ENABLED)
85 
86  timers_enabled = false;
87  // for(int i=0; i<10;i++)
88  //{
89  // perf_timers[0].stop();
90  //}
91  for (int i = 0; i < TIMER_COUNT; i++) {
92  std::cout << timer_messages[i] << " " << int(perf_data[i] / 1000) << " "
93  << call_count[i] << "\n";
94  }
95 #else
96  volatile int a = 0;
97  asm volatile("nop");
98 
99 #endif
100 
101  return 0;
102 }
103 
104 #if (TIME_PROFILE_ENABLED)
105 //==============================================================================
106 Timer::Timer() {
107 #ifdef _WIN32
108  QueryPerformanceFrequency(&frequency);
109  startCount.QuadPart = 0;
110  endCount.QuadPart = 0;
111 #else
112  startCount.tv_sec = startCount.tv_usec = 0;
113  endCount.tv_sec = endCount.tv_usec = 0;
114 #endif
115 
116  stopped = 0;
117  startTimeInMicroSec = 0;
118  endTimeInMicroSec = 0;
119 }
120 
121 //==============================================================================
122 Timer::~Timer() {
123  // Do nothing
124 }
125 
126 //==============================================================================
127 void Timer::start() {
128  stopped = 0; // reset stop flag
129 #ifdef _WIN32
130  QueryPerformanceCounter(&startCount);
131 #else
132  clock_gettime(CLOCK_MONOTONIC, &time_start);
133  // gettimeofday(&startCount, nullptr);
134 #endif
135 }
136 
137 //==============================================================================
138 void Timer::stop() {
139  stopped = 1; // set timer stopped flag
140 
141 #ifdef _WIN32
142  QueryPerformanceCounter(&endCount);
143 #else
144  clock_gettime(CLOCK_MONOTONIC, &time_end);
145  // gettimeofday(&endCount, nullptr);
146 #endif
147 }
148 unsigned long Timer::getElapsedTimeInNanoSec() {
149  if (!stopped) clock_gettime(CLOCK_MONOTONIC, &time_end);
150  timespec time_diff;
151  time_diff = diff(time_start, time_end);
152 
153  if (time_diff.tv_sec) {
154  return time_diff.tv_sec * 10000000000 + time_diff.tv_nsec;
155  } else {
156  return time_diff.tv_nsec;
157  }
158 }
160 #ifdef _WIN32
161  if (!stopped) QueryPerformanceCounter(&endCount);
162 
163  startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
164  endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
165 #else
166  if (!stopped) clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time_end);
167  // gettimeofday(&endCount, nullptr);
168 
169  // startTimeInMicroSec = (startCount.tv_sec * 1000000.0) +
170  // startCount.tv_usec; endTimeInMicroSec = (endCount.tv_sec * 1000000.0) +
171  // endCount.tv_usec;
172 #endif
173  timespec time_diff;
174  time_diff = diff(time_start, time_end);
175 
176  if (time_diff.tv_sec) {
177  return time_diff.tv_sec * 10000000000 + time_diff.tv_nsec / 1000;
178  } else {
179  return time_diff.tv_nsec / 1000;
180  }
181 }
182 
183 //==============================================================================
185  return this->getElapsedTimeInMicroSec() * 0.001;
186 }
187 
188 //==============================================================================
190  return this->getElapsedTimeInMicroSec() * 0.000001;
191 }
192 
193 //==============================================================================
194 double Timer::getElapsedTime() { return this->getElapsedTimeInMilliSec(); }
195 
196 timespec diff(timespec start, timespec end) {
197  timespec temp;
198  if ((end.tv_nsec - start.tv_nsec) < 0) {
199  temp.tv_sec = end.tv_sec - start.tv_sec - 1;
200  temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
201  } else {
202  temp.tv_sec = end.tv_sec - start.tv_sec;
203  temp.tv_nsec = end.tv_nsec - start.tv_nsec;
204  }
205  return temp;
206 }
207 #endif
208 
209 } // namespace test
double getElapsedTimeInSec()
double getElapsedTimeInMilliSec()
get elapsed time in milli-second
double getElapsedTime()
get elapsed time in milli-second
unsigned long getElapsedTimeInNanoSec()
get elapsed time in micro-second
int init_perfomance_timers()
void stop_timer(int index)
void start()
start timer
int report_perfomance()
timespec diff(timespec start, timespec end)
void stop()
stop the timer
double getElapsedTimeInMicroSec()
get elapsed time in micro-second
void start_timer(int index)