32 #ifndef VSMC_UTILITY_STOP_WATCH_HPP
33 #define VSMC_UTILITY_STOP_WATCH_HPP
37 #if VSMC_HAS_CXX11LIB_CHRONO
44 #ifndef VSMC_STOP_WATCH_CHRONO_CLOCK_TYPE
45 #define VSMC_STOP_WATCH_CHRONO_CLOCK_TYPE std::chrono::high_resolution_clock
50 #ifndef VSMC_STOP_WATCH_TYPE
51 #if VSMC_HAS_CXX11LIB_CHRONO
52 #define VSMC_STOP_WATCH_TYPE ::vsmc::StopWatchChrono
53 #elif defined(VSMC_MACOSX) || VSMC_HAS_POSIX || defined(VSMC_MSVC)
54 #define VSMC_STOP_WATCH_TYPE ::vsmc::StopWatchSYS
56 #define VSMC_STOP_WATCH_TYPE ::vsmc::StopWatchNull
60 #if defined(VSMC_MACOSX)
61 #include <mach/mach_time.h>
64 #elif defined(VSMC_MSVC)
72 template <
typename WatchType>
80 start_(start), watch_(watch) {
if (start_) watch_.start();}
109 double hours ()
const {
return 1
e-9 / 3600;}
112 #if VSMC_HAS_CXX11LIB_CHRONO
116 template <
typename ClockType>
144 start_time_ = clock_type::now();
159 typename clock_type::time_point stop_time = clock_type::now();
160 elapsed_ += stop_time - start_time_;
170 elapsed_ =
typename clock_type::duration(0);
177 return std::chrono::duration_cast<std::chrono::duration<
178 double, std::nano> >(elapsed_).count();
184 return std::chrono::duration_cast<std::chrono::duration<
185 double, std::micro> >(elapsed_).count();
191 return std::chrono::duration_cast<std::chrono::duration<
192 double, std::milli> >(elapsed_).count();
198 return std::chrono::duration_cast<std::chrono::duration<
199 double, std::ratio<1> > >(elapsed_).count();
205 return std::chrono::duration_cast<std::chrono::duration<
206 double, std::ratio<60> > >(elapsed_).count();
212 return std::chrono::duration_cast<std::chrono::duration<
213 double, std::ratio<3600> > >(elapsed_).count();
218 typename clock_type::duration elapsed_;
219 typename clock_type::time_point start_time_;
225 typedef StopWatchClockAdapter<VSMC_STOP_WATCH_CHRONO_CLOCK_TYPE>
228 #endif // VSMC_HAS_CXX11LIB_CHRONO
230 #if defined(VSMC_MACOSX)
252 start_time_ = ::mach_absolute_time();
262 uint64_t stop_time = ::mach_absolute_time();
263 uint64_t elapsed_abs = stop_time - start_time_;
264 uint64_t elapsed_nsec = elapsed_abs *
265 timebase_.numer / timebase_.denom;
266 uint64_t inc_sec = elapsed_nsec / ratio_;
267 uint64_t inc_nsec = elapsed_nsec % ratio_;
268 elapsed_sec_ += inc_sec;
269 elapsed_nsec_ += inc_nsec;
280 ::mach_timebase_info(&timebase_);
285 {
return elapsed_sec_ * 1e9 + elapsed_nsec_;}
288 {
return elapsed_sec_ * 1e6 + elapsed_nsec_ * 1
e-3;}
291 {
return elapsed_sec_ * 1e3 + elapsed_nsec_ * 1
e-6;}
294 {
return elapsed_sec_ + elapsed_nsec_ * 1
e-9;}
304 uint64_t elapsed_sec_;
305 uint64_t elapsed_nsec_;
306 uint64_t start_time_;
307 ::mach_timebase_info_data_t timebase_;
310 static_cast<uint64_t
>(1000000000ULL);
321 bool running () {
return running_;}
329 ::clock_gettime(CLOCK_REALTIME, &start_time_);
340 ::clock_gettime(CLOCK_REALTIME, &stop_time);
341 time_t sec = stop_time.tv_sec - start_time_.tv_sec;
342 long nsec = stop_time.tv_nsec - start_time_.tv_nsec;
344 time_t inc_sec = sec + nsec / ratio_;
345 long inc_nsec = nsec % ratio_;
346 elapsed_.tv_sec += inc_sec;
347 elapsed_.tv_nsec += inc_nsec;
357 elapsed_.tv_nsec = 0;
362 {
return elapsed_.tv_sec * 1e9 + elapsed_.tv_nsec;}
365 {
return elapsed_.tv_sec * 1e6 + elapsed_.tv_nsec * 1
e-3;}
368 {
return elapsed_.tv_sec * 1e3 + elapsed_.tv_nsec * 1
e-6;}
371 {
return elapsed_.tv_sec + elapsed_.tv_nsec * 1
e-9;}
376 double hours ()
const
382 ::timespec start_time_;
387 #elif defined(VSMC_MSVC)
394 elapsed_(0), start_time_(0), frequency_(0), running_(false)
397 bool running () {
return running_;}
406 ::QueryPerformanceCounter(&time);
407 start_time_ = time.QuadPart;
418 ::QueryPerformanceCounter(&time);
419 elapsed_ += time.QuadPart - start_time_;
430 ::QueryPerformanceFrequency(&freq);
431 frequency_ = freq.QuadPart;
436 {
return elapsed_ / (frequency_ * 1
e-9);}
439 {
return elapsed_ / (frequency_ * 1
e-6);}
442 {
return elapsed_ / (frequency_ * 1
e-3);}
445 {
return elapsed_ /
static_cast<double>(frequency_);}
450 double hours ()
const
461 #endif // defined(VSMC_MACOSX)
469 #endif // VSMC_UTILITY_STOP_WATCH_HPP
::vsmc::StopWatchChrono StopWatch
The default StopWatch.
#define VSMC_CONSTEXPR
constexpr
double nanoseconds() const
double nanoseconds() const
Return the accumulated elapsed time in nanoseconds.
double microseconds() const
double hours() const
Return the accumulated elapsed time in hours.
void reset()
Stop and reset the elapsed time to zero.
double microseconds() const
StopWatchGuard(watch_type &watch, bool start=true)
double minutes() const
Return the accumulated elapsed time in minutes.
double milliseconds() const
StopWatch as an adapter of C++11 clock.
bool stop()
Stop the watch, no effect if already stopped.
double microseconds() const
Return the accumulated elapsed time in microseconds.
double milliseconds() const
Return the accumulated elapsed time in milliseconds.
Stop watch using system native API.
#define VSMC_STOP_WATCH_TYPE
Default StopWatch type.
double milliseconds() const
bool start()
Start the watch, no effect if already started.
double nanoseconds() const
Start and stop a StopWatch in scope (similiar to a mutex lock guard)
StopWatchClockAdapter< std::chrono::high_resolution_clock > StopWatchChrono
Stop watch using
bool running() const
If the watch is running.
double seconds() const
Return the accumulated elapsed time in seconds.