复制程式
//计算所花时间(微秒) GCC
#include <stdio.h>
#include <sys/time.h>
void tvsub(struct timeval *tdiff,
const struct timeval *t1,
const struct timeval *t0)
{
tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
if (tdiff->tv_usec < 0)
tdiff->tv_sec--, tdiff->tv_usec += 1000000;
}
void print_time(const struct timeval *t0, const struct timeval *t1)
{
struct timeval td;
double s;
tvsub(&td, t1, t0);
s = td.tv_sec + (td.tv_usec / 1000000.);
printf("It cost %.3lf secs.\n", s);
}
int main(void)
{
struct timeval start, stop;
int i;
gettimeofday(&start, (struct timezone *)0);
for(i=0; i<10000; i++) printf("%d\n", i);
//do something here!!!!
gettimeofday(&stop, (struct timezone *)0);
print_time(&start, &stop);
return 0;
}