diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-09-20 23:08:17 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-09-20 23:24:18 +0200 |
commit | b3e0d66eeea9f480ba503ea8aaf53b87dec2a5f7 (patch) | |
tree | fd17ec16930ca99fdc59451677c1efefe756b280 /opentrack-compat | |
parent | 88e4563455ca183e1b77364b73d5e6173af513d3 (diff) |
compat/timer: backport, update usages
Diffstat (limited to 'opentrack-compat')
-rw-r--r-- | opentrack-compat/timer.hpp | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/opentrack-compat/timer.hpp b/opentrack-compat/timer.hpp index f0741295..28de1700 100644 --- a/opentrack-compat/timer.hpp +++ b/opentrack-compat/timer.hpp @@ -24,10 +24,14 @@ static inline void opentrack_clock_gettime(int, struct timespec* ts) (void) QueryPerformanceCounter(&d); - long long part = d.QuadPart / ((long double)freq.QuadPart) * 1000000000.L; + using ll = long long; + using ld = long double; + const long long part = ll(d.QuadPart / ld(freq.QuadPart) * 1000000000.L); + using t_s = decltype(ts->tv_sec); + using t_ns = decltype(ts->tv_nsec); - ts->tv_sec = part / 1000000000ULL; - ts->tv_nsec = part % 1000000000ULL; + ts->tv_sec = t_s(part / 1000000000LL); + ts->tv_nsec = t_ns(part % 1000000000LL); } # define clock_gettime opentrack_clock_gettime #else @@ -49,26 +53,45 @@ static inline void clock_gettime(int, struct timespec* ts) } # endif #endif -class Timer { +class Timer +{ private: struct timespec state; - long long conv(const struct timespec& cur) const + long long conv_nsecs(const struct timespec& cur) const { return (cur.tv_sec - state.tv_sec) * 1000000000LL + (cur.tv_nsec - state.tv_nsec); } + long conv_usecs(const struct timespec& cur) const + { + return long(cur.tv_sec - state.tv_sec) * 1000000L + long(cur.tv_nsec - state.tv_nsec) / 1000l; + } public: - Timer() { + Timer() + { start(); } - void start() { - (void) clock_gettime(CLOCK_MONOTONIC, &state); + void start() + { + clock_gettime(CLOCK_MONOTONIC, &state); + } + long long elapsed_nsecs() const + { + struct timespec cur; + clock_gettime(CLOCK_MONOTONIC, &cur); + return conv_nsecs(cur); } - long long elapsed() const { + long elapsed_usecs() const + { struct timespec cur; - (void) clock_gettime(CLOCK_MONOTONIC, &cur); - return conv(cur); + clock_gettime(CLOCK_MONOTONIC, &cur); + return long(conv_usecs(cur)); } - long elapsed_ms() const { - return elapsed() / 1000000L; + long elapsed_ms() const + { + return elapsed_usecs() / 1000L; + } + double elapsed_seconds() const + { + return double(elapsed_nsecs() * 1e-9L); } }; |