- struct timeval
- {
- __time_t tv_sec; /* Seconds. */
- __suseconds_t tv_usec; /* Microseconds. */
- };
其中,tv_sec爲Epoch到創建struct timeval時的秒數,tv_usec爲微秒數,即秒後面的零頭。比如當前我寫博文時的tv_sec爲1244770435,tv_usec爲442388,即當前時間距Epoch時間1244770435秒,442388微秒。需要注意的是,因爲循環過程,新建結構體變量等過程需消耗部分時間,我們作下面的運算時會得到如下結果:
- #include <sys/time.h>
- #include <stdio.h>
- int
- main(void)
- {
- int i;
- struct timeval tv;
- for(i = 0; i < 4; i++){
- gettimeofday(&tv, NULL);
- printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
- sleep(1);
- }
- return 0;
- }
結果如下:
- 329612 1314851429
- 329782 1314851430
- 329911 1314851431
- 330036 1314851432