X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/loba.git/blobdiff_plain/6f5ec5fdc42f96a8fe95f4b846b163d4dc92e0c8..8f4688912eba3ef24340c76b4db4a336b04a7c66:/timer.h?ds=sidebyside diff --git a/timer.h b/timer.h index dbf32e1..ccb115b 100644 --- a/timer.h +++ b/timer.h @@ -4,24 +4,31 @@ #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; } @@ -29,63 +36,102 @@ inline struct timeval operator-(const struct timeval& a, const struct timeval& b) { 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 struct 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