Debian

time diff


參考資訊:
1. how-to-print-time-difference-in-accuracy-of-milliseconds-and-nanoseconds-from-c

方法一

uint32_t diff=0;
struct timeval tv1={0}, tv2={0};
const uint32_t TICKS_PER_SEC=1000000;

gettimeofday(&tv1, 0);
//...
gettimeofday(&tv2, 0);
diff = ((tv2.tv_sec - tv1.tv_sec) * TICKS_PER_SEC) + (tv2.tv_usec - tv1.tv_usec);

方法二

struct timespec tstart={0,0}, tend={0,0};

clock_gettime(CLOCK_MONOTONIC, &tstart);
//...
clock_gettime(CLOCK_MONOTONIC, &tend);
printf("%.5fs\n", ((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) - ((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec));


返回上一頁