1.簡介:
在Linux中可以使用函數do_gettimeofday()函數來得到精確時間。它的精度可以達到微妙,是與C標准庫中gettimeofday()用發相同的函數。在Linux內核中獲得時間的函數。
2.函數原型:
#include <linux/time.h> void do_gettimeofday(struct timeval *tv);
3.說明:
do_gettimeofday()會把目前的時間用tv 結構體返回,當地時區的信息則放到tz所指的結構中
4.結構體:
1. timeval 結構體定義:
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
5.例
struct timeval tv_begin,tv_end; do_gettimeofday(&tv_begin,NULL); sleep(5); do_gettimeofday(&tv_end,NULL); printf(“tv_begin_sec:%d\n”,tv_begin.tv_sec); printf(“tv_begin_usec:%d\n”,tv_begin.tv_usec); printf(“tv_end_sec:%d\n”,tv_end.tv_sec); printf(“tv_end_usec:%d\n”,tv_end.tv_usec);