Linux计算时间间隔


Linux计算时间间隔:
 
#include
#include

/*
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
strut timeval {
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数 */
};
*/

struct timeval start,end;
int timeuse;

void time()
{
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
timeuse /= 1000000;
printf("time used: %ds\n", timeuse);
}

void foo()
{
int i=0;
for(;i<10000;i++);
}

int main()
{
gettimeofday( &start, NULL );
foo();
time();
return 0;
}

相关内容