[an error occurred while processing this directive]
[an error occurred while processing this directive]struct timeval tv; ... gettimeofday(&tv, NULL);tv.tv_sec に秒が、tv.tv_usec にマイクロ秒が入っています。
struct timeval tv0, tv1; ... gettimeofday(&tv0, NULL); a(); gettimeofday(&tv1, NULL);としたあとに、(tv1 - tv0) なんて引き算は出来ません。 (tv1.tv_sec - tv0.tv_sec) * 1000 * 1000 + (tv1.tv_usec - tv0.tv_usec) として、マイクロ秒を計算します。
double get_dtime(void){
struct timeval tv;
gettimeofday(&tv, NULL);
return ((double)(tv.tv_sec) + (double)(tv.tv_usec) * 0.001 * 0.001);
}
gcc なら inline つけたほうがいいですね。
double d0, d1;
d0 = get_dtime();
a();
d1 = get_dtime();
printf("elapsed: %f", d1 - d0);
(taiyop さん、ミスのご指摘ありがとうございました)
double d[5];
int i = 0;
d[i++] = get_dtime();
a();
d[i++] = get_dtime();
b();
d[i++] = get_dtime();
c();
d[i++] = get_dtime();
d();
d[i++] = get_dtime();
printf("a(): %f\n", d[1] - d[0]);
printf("b(): %f\n", d[2] - d[1]);
printf("c(): %f\n", d[3] - d[2]);
printf("d(): %f\n", d[4] - d[3]);