2013.1.12
示例程序:
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>//sleep函数
/*
struct timeval {
time_t tv_sec;// seconds
time_t tv_usec; // microseconds
};
*/
//它获得的时间精确到微秒(1e-6 s)量级
int main(int argc, char * argv[])
{
struct timeval tv_start,tv_end;
gettimeofday(&tv_start, NULL);
sleep(10);
//dosomething
gettimeofday(&tv_end, NULL);
double timediff=( tv_end.tv_sec- tv_start.tv_sec)*1000000+ ( tv_end.tv_usec- tv_start.tv_usec);
int timediffs=timediff/1000000;
printf("执行时间为=%f",timediff);
printf("毫秒\n");
printf("约等于=%d秒\n",timediffs);
return 0;
}