struct timeval {
long tv_sec; // seconds
long tv_usec; // and microseconds(百万分之一秒)
};
timeval结构的用于指定时间值。
头文件#include <sys/time.h>
测试代码
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#define MAX_VALUE 1000000
#define MSECOND 1000000
int main(void) {
unsigned long long a;
unsigned long long sum = 0;
float timeuse = 0;
struct timeval tpstart,tpend;
gettimeofday(&tpstart,NULL);
for(a = 0; a < MAX_VALUE; a++)
sum += a;
gettimeofday(&tpend,NULL);
timeuse = MSECOND * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec;
timeuse /= MSECOND;
printf("the value od sum is:%llu,Used Time:%f\n",sum,timeuse);
return 0;
}
结果:the value od sum is:499999500000,Used Time:0.004108