参考网址:
(131条消息) clock_gettime 函数笔记_神秘的羔羊的博客-CSDN博客_clock_gettime
clock_gettime
1 2 3 4 5 6 7 8 9 10 11 12
| #include<time.h> int clock_gettime(clockid_t clk_id,struct timespec *tp);
struct timespec { time_t tv_sec; long tv_nsec; }
|
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <time.h> #include <stdio.h> #include <unistd.h> int main(int argc, char **argv) { struct timespec time1 = {0, 0}; struct timespec time2 = {0, 0}; float temp; clock_gettime(CLOCK_REALTIME, &time1); usleep(1000); clock_gettime(CLOCK_REALTIME, &time2); temp = (time2.tv_nsec - time1.tv_nsec) / 1000000; printf("time = %f ms\n", temp); return 0; }
|