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

            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可以精确到纳秒。