X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/loba.git/blobdiff_plain/147d0e88c4da14c801d15e0875bedb474180bea2..7aedebde4d8de8a7b9f0d3ba2dc38f968c739569:/timer.h?ds=sidebyside diff --git a/timer.h b/timer.h index 0e8c7d9..ccb115b 100644 --- a/timer.h +++ b/timer.h @@ -4,88 +4,134 @@ #include #include -#if NEED_TIMERCLEAR -inline -void timerclear(struct timeval& a) -{ - tv.sec = tv.usec = 0; -} +#ifdef _BSD_SOURCE +# define HAVE_TIMERADD +# define HAVE_TIMERSUB +# define HAVE_TIMERCLEAR +#else +# warning _BSD_SOURCE not defined +# undef HAVE_TIMERADD +# undef HAVE_TIMERSUB +# undef HAVE_TIMERCLEAR #endif inline struct timeval operator+(const struct timeval& a, const struct timeval& b) { struct timeval result; +#ifdef HAVE_TIMERADD + timeradd(&a, &b, &result); +#else result.tv_sec = a.tv_sec + b.tv_sec; result.tv_usec = a.tv_usec + b.tv_usec; if (result.tv_usec >= 1000000) { ++result.tv_sec; result.tv_usec -= 1000000; } +#endif return result; } inline struct timeval operator-(const struct timeval& a, const struct timeval& b) { - timeval result; + struct timeval result; +#ifdef HAVE_TIMERSUB + timersub(&a, &b, &result); +#else result.tv_sec = a.tv_sec - b.tv_sec; result.tv_usec = a.tv_usec - b.tv_usec; if (result.tv_usec < 0) { - -- result.tv_sec; + --result.tv_sec; result.tv_usec += 1000000; } +#endif return result; } -double timertod(const timeval& a) -{ - return a.tv_sec + a.tv_usec / 1e6; -} - class timestamp { +public: + timestamp(); + ~timestamp(); + void reset(); + + void start(); + void stop(); + + struct timeval tv_duration() const; + double duration() const; + private: struct rusage before; struct rusage after; struct timeval difference; -public: - timestamp() - { - reset(); - } + static void tv_clear(struct timeval& a); + static double timertod(const struct timeval& a); +}; - void reset() - { - timerclear(&before.ru_utime); - timerclear(&before.ru_stime); - timerclear(&after.ru_utime); - timerclear(&after.ru_stime); - timerclear(&difference); - } +inline +timestamp::timestamp() +{ + reset(); +} - void start() - { - getrusage(RUSAGE_SELF, &before); - } +inline +timestamp::~timestamp() +{ +} - void stop() - { - getrusage(RUSAGE_SELF, &after); - difference = difference + ((after.ru_utime + after.ru_stime) - - (before.ru_utime + before.ru_stime)); - } +inline +void timestamp::reset() +{ + tv_clear(before.ru_utime); + tv_clear(before.ru_stime); + tv_clear(after.ru_utime); + tv_clear(after.ru_stime); + tv_clear(difference); +} - struct timeval tv_duration() const - { - return difference; - } +inline +void timestamp::start() +{ + getrusage(RUSAGE_SELF, &before); +} - double duration() const - { - return timertod(difference); - } -}; +inline +void timestamp::stop() +{ + getrusage(RUSAGE_SELF, &after); + difference = difference + ((after.ru_utime + after.ru_stime) - + (before.ru_utime + before.ru_stime)); +} + +inline +struct timeval timestamp::tv_duration() const +{ + return difference; +} + +inline +double timestamp::duration() const +{ + return timertod(difference); +} + +inline +void timestamp::tv_clear(struct timeval& a) +{ +#ifdef HAVE_TIMERCLEAR + timerclear(&a); +#else + tv.sec = tv.usec = 0; +#endif +} + +inline +double timestamp::timertod(const struct timeval& a) +{ + return a.tv_sec + a.tv_usec / 1e6; +} #endif // !TIMER_H