欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

clock_gettime获取时间

发布时间:2025/6/15 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 clock_gettime获取时间 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

            ❤️强烈推荐人工智能学习网站❤️

            linux下clock_getting可以用来获取时间并精度到纳秒,其中需要用到struct timespec结构体,struct timespec结构体如下:

struct timespec {time_t tv_sec;long tv_nsec; }


写个小程序测试一下:

#include<stdio.h> #include<time.h> #include<sys/time.h>void test_time() {struct timespec time;clock_gettime(CLOCK_REALTIME,&time);printf("tv_sec=%ld,tv_nsec=%ld\n",time.tv_sec,time.tv_nsec); }int main() {test_time();return 0; }

编译运行:

 

[mapan@localhost thread]$ gcc -lrt time1.c [mapan@localhost thread]$ ./a.out tv_sec=1509774203,tv_nsec=413767266 [mapan@localhost thread]$


linux下还有其他的时间结构体,但是struct timespec可以精确到纳秒。

 

 

 

 

 

 

 

总结

以上是生活随笔为你收集整理的clock_gettime获取时间的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。