summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-08-10 09:49:27 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-08-10 10:42:01 +0200
commitd4a9bb0b29c26de216f7d0da34397dfbc5a592df (patch)
treed3d910bf250760912c40bb433c8f2c14786ee635
parent30f659b74a063677954b23c405be3b7229b10856 (diff)
compat/timer: allow for converting usecs -> long
-rw-r--r--opentrack-compat/timer.hpp35
1 files changed, 25 insertions, 10 deletions
diff --git a/opentrack-compat/timer.hpp b/opentrack-compat/timer.hpp
index 9523c8c9..300a883c 100644
--- a/opentrack-compat/timer.hpp
+++ b/opentrack-compat/timer.hpp
@@ -27,8 +27,9 @@ static inline void opentrack_clock_gettime(int, struct timespec* ts)
(void) QueryPerformanceCounter(&d);
- using t = long long;
- const long long part = t(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);
@@ -59,27 +60,41 @@ 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 start()
+ {
clock_gettime(CLOCK_MONOTONIC, &state);
}
- long long elapsed() const {
+ long long elapsed_nsecs() const
+ {
+ struct timespec cur;
+ clock_gettime(CLOCK_MONOTONIC, &cur);
+ return conv_nsecs(cur);
+ }
+ long elapsed_usecs() const
+ {
struct timespec cur;
clock_gettime(CLOCK_MONOTONIC, &cur);
- return conv(cur);
+ return long(conv_usecs(cur));
}
- long elapsed_ms() const {
- return long(elapsed() / 1000000LL);
+ long elapsed_ms() const
+ {
+ return elapsed_usecs() / 1000L;
}
double elapsed_seconds() const
{
- return elapsed() * 1e-9;
+ return double(elapsed_nsecs() * 1e-9L);
}
};