二.gettimeofday()函数的一个常用方法
在测试程序时,往往需要了解程序执行所需的时间,在Linux中可以使用函数gettimeofday来得到时间.
1.程序实例:
测试调用delya()函数所需执行的时间(单位为微妙)
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
int delay(int time)
{
int i,j;
for(i =0;i<time;i++)
for(j=0;j<5000;j++)
;
}
int main()
{
struct timeval start;
struct timeval end;
unsigned long diff;
gettimeofday(&start,NULL);
delay(10);
gettimeofday(&end,NULL);
diff = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
printf(“thedifference is %ld\n”,diff);
return 0;
}
说明:
使用该方法就可以检测出调用delay()函数所使用的时间
