在Linux中可使用函數do_gettimeofday()函數來獲得精確時間。它的精度能夠達到微妙,是與C標準庫中gettimeofday()用發相同的函數。在Linux內核中得到時間的函數。linux
#include <linux/time.h> void do_gettimeofday(struct timeval *tv);
do_gettimeofday()會把目前的時間用tv 結構體返回,當地時區的信息則放到tz所指的結構中函數
1. timeval 結構體定義:spa
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
5.例code
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);