/* speed.c purpose: how fast are a half million sqrts. comp/link: gcc -o speed speed.c -lm */ #define NSQRTS 500000 #include #include #include #include /******************************************************************************/ int main(int argc, char **argv) { int i; double ival; double time1, time2; struct timeval tv1, tv2; struct timezone tz1, tz2; gettimeofday(&tv1, &tz1); for(i = 0; i < NSQRTS; i++) ival = sqrt((double)i); gettimeofday(&tv2, &tz2); /* printf("second %d, micro second %d\n", tv1.tv_sec, tv1.tv_usec); */ /* printf("second %d, micro second %d\n", tv2.tv_sec, tv2.tv_usec); */ time1 = (double)tv1.tv_sec + (double)tv1.tv_usec/1.0e6; time2 = (double)tv2.tv_sec + (double)tv2.tv_usec/1.0e6; printf("%d square roots in ... \n", NSQRTS); printf("time2 - time1 = %lf - %lf = %lf s\n", time2, time1, time2 - time1); return 0; } /* main */