32 #ifndef MCKL_UTILITY_STOP_WATCH_HPP 33 #define MCKL_UTILITY_STOP_WATCH_HPP 49 unsigned c = 0x40000001;
50 __asm__
volatile(
"rdpmc" :
"=a"(a),
"=d"(d) :
"c"(c));
52 return (static_cast<std::uint64_t>(d) << 32) + a;
59 unsigned c = 0x40000001;
60 __asm__
volatile(
"rdpmc" :
"=a"(a),
"=d"(d) :
"c"(c));
62 return (static_cast<std::uint64_t>(d) << 32) + a;
76 :
"=r"(hi),
"=r"(lo)::
"%eax",
"%ebx",
"%ecx",
"%edx");
77 return (static_cast<std::uint64_t>(hi) << 32) + lo;
89 :
"=r"(hi),
"=r"(lo)::
"%eax",
"%ebx",
"%ecx",
"%edx");
90 return (static_cast<std::uint64_t>(hi) << 32) + lo;
104 :
"=r"(hi),
"=r"(lo)::
"%eax",
"%ebx",
"%ecx",
"%edx");
105 return (static_cast<std::uint64_t>(hi) << 32) + lo;
117 :
"=r"(hi),
"=r"(lo)::
"%eax",
"%ebx",
"%ecx",
"%edx");
118 return (static_cast<std::uint64_t>(hi) << 32) + lo;
121 #else // MCKL_USE_RDPMC 127 #endif // MCKL_USE_RDPMC 135 template <
typename WatchType>
142 : watch_(watch), start_(start)
165 template <
typename ClockType = std::chrono::high_resolution_clock>
172 : time_(0), cycles_(0), cycles_start_(0), running_(false)
184 #if MCKL_USE_RDPMC || MCKL_USE_RDTSCP || MCKL_USE_RDTSC 211 time_start_ = clock_type::now();
225 typename clock_type::time_point t = clock_type::now();
232 cycles_ += c - cycles_start_;
233 time_ += t - time_start_;
242 time_ =
typename clock_type::duration(0);
251 typename clock_type::duration
const time()
const {
return time_; }
254 template <
typename Rep,
typename Period>
257 using time_type = std::chrono::duration<Rep, Period>;
259 return std::chrono::duration_cast<time_type>(time_).count();
272 double seconds()
const {
return time<double, std::ratio<1>>(); }
275 double minutes()
const {
return time<double, std::ratio<60>>(); }
278 double hours()
const {
return time<double, std::ratio<3600>>(); }
281 typename clock_type::duration time_;
282 typename clock_type::time_point time_start_;
295 #endif // MCKL_UTILITY_STOP_WATCH_HPP double seconds() const
Equivalent to time<double, std::ratio<1>>()
bool start()
Start the watch, no effect if already started.
double hours() const
Equivalent to time<double, std::ratio<3600>>()
#define MCKL_PUSH_CLANG_WARNING(warning)
double nanoseconds() const
Equivalent to time<double, std::nano>()
std::uint64_t cycle_start()
double microseconds() const
Equivalent to time<double, std::micro>()
bool running() const
If the watch is running.
std::uint64_t cycles() const
Return the accumulated cycles.
Rep time() const
Return the accumulated elapsed time in specified format.
bool stop()
Stop the watch, no effect if already stopped.
std::uint64_t cycle_stop()
static constexpr bool has_cycles()
If cycle counting is supported.
double milliseconds() const
Equivalent to time<double, std::milli>()
void reset()
Stop and reset the elapsed time to zero.
Start and stop a StopWatch in scope (similiar to a mutex lock guard)
double minutes() const
Equivalent to time<double, std::ratio<60>>()
#define MCKL_POP_CLANG_WARNING
clock_type::duration const time() const
Return the accumulated elapsed time in its native format.
StopWatchGuard(watch_type &watch, bool start=true)
StopWatch as an adapter of C++ standard library compatible clock.