Linux下struct timeval結構體

  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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章