Linux下struct timeval结构体

来源:互联网 发布:windows不能识别usb 编辑:程序博客网 时间:2024/06/04 01:07
转载自:http://blog.chinaunix.net/uid-20548989-id-2533161.html
  1. struct timeval  
  2. {  
  3. __time_t tv_sec;        /* Seconds. */  
  4. __suseconds_t tv_usec;  /* Microseconds. */  
  5. };  

其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。比如当前我写博文时的tv_sec为1244770435,tv_usec为442388,即当前时间距Epoch时间1244770435秒,442388微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:

[cpp] view plain copy
  1. #include <sys/time.h>  
  2. #include <stdio.h>  
  3.     
  4. int  
  5. main(void)  
  6. {  
  7.         int i;  
  8.         struct timeval tv;  
  9.   
  10.         for(i = 0; i < 4; i++){  
  11.                 gettimeofday(&tv, NULL);  
  12.                 printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);  
  13.                 sleep(1);  
  14.         }  
  15.   
  16.         return 0;  
  17. }  

结果如下:

[cpp] view plain copy
  1. 329612  1314851429  
  2. 329782  1314851430  
  3. 329911  1314851431  
  4. 330036  1314851432
原创粉丝点击