1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <limits.h>
#include "assert.h"
#include "timer.h"
/* Save current time in 'time'. */
static void timerNow(struct timespec *time)
{
int rv;
rv = clock_gettime(CLOCK_MONOTONIC, time);
assert(rv == 0);
}
void TimerStart(struct timer *timer)
{
timerNow(&timer->start);
}
unsigned long TimerStop(struct timer *timer)
{
struct timespec now;
time_t nsecs;
timerNow(&now);
if (timer->start.tv_sec == now.tv_sec) {
nsecs = now.tv_nsec - timer->start.tv_nsec;
} else {
nsecs = (now.tv_sec - timer->start.tv_sec) * 1000 * 1000 * 1000 -
timer->start.tv_nsec + now.tv_nsec;
}
return (unsigned long)nsecs;
}
|