32 #ifndef VSMC_UTILITY_STOP_WATCH_HPP 33 #define VSMC_UTILITY_STOP_WATCH_HPP 43 #ifndef VSMC_STOP_WATCH_CLOCK_TYPE 44 #define VSMC_STOP_WATCH_CLOCK_TYPE std::chrono::high_resolution_clock 57 #if defined(VSMC_CLANG) || defined(VSMC_GCC) || defined(VSMC_INTEL) 61 asm volatile(
"CPUID\n\t" 65 :
"=r"(hi),
"=r"(lo)::
"%rax",
"%rbx",
"%rcx",
"%rdx");
66 #else // VSMC_HAS_X64_64 67 asm volatile(
"CPUID\n\t" 71 :
"=r"(lo),
"=r"(lo)::
"%eax",
"%ebx",
"%ecx",
"%edx");
72 #endif // VSMC_HAS_X86_64 73 return (static_cast<std::uint64_t>(hi) << 32) + lo;
74 #elif defined(VSMC_MSVC) 76 #else // defined(VSMC_CLANG) || defined(VSMC_GCC) || defined(VSMC_INTEL) 78 #endif // defined(VSMC_CLANG) || defined(VSMC_GCC) || defined(VSMC_INTEL) 82 #endif // VSMC_HAS_X86 90 template <
typename WatchType>
97 : start_(start), watch_(watch)
116 template <
typename ClockType>
123 : time_(0), cycles_(0), cycles_start_(0), running_(false)
147 time_start_ = clock_type::now();
164 typename clock_type::time_point time_stop = clock_type::now();
165 time_ += time_stop - time_start_;
175 time_ =
typename clock_type::duration(0);
181 double cycles()
const {
return cycles_; }
186 return std::chrono::duration_cast<
187 std::chrono::duration<double, std::nano>>(time_)
194 return std::chrono::duration_cast<
195 std::chrono::duration<double, std::micro>>(time_)
202 return std::chrono::duration_cast<
203 std::chrono::duration<double, std::milli>>(time_)
210 return std::chrono::duration_cast<
211 std::chrono::duration<double, std::ratio<1>>>(time_)
218 return std::chrono::duration_cast<
219 std::chrono::duration<double, std::ratio<60>>>(time_)
226 return std::chrono::duration_cast<
227 std::chrono::duration<double, std::ratio<3600>>>(time_)
232 typename clock_type::duration time_;
233 typename clock_type::time_point time_start_;
245 #endif // VSMC_UTILITY_STOP_WATCH_HPP
std::chrono::high_resolution_clock clock_type
double cycles() const
Return the accumulated cycles.
double nanoseconds() const
Return the accumulated elapsed time in nanoseconds.
double hours() const
Return the accumulated elapsed time in hours.
void reset()
Stop and reset the elapsed time to zero.
StopWatchGuard(watch_type &watch, bool start=true)
double minutes() const
Return the accumulated elapsed time in minutes.
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.
bool start()
Start the watch, no effect if already started.
Start and stop a StopWatch in scope (similiar to a mutex lock guard)
bool running() const
If the watch is running.
double seconds() const
Return the accumulated elapsed time in seconds.