1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <stdio.h>
#include <sys/time.h>
#include <math.h>
int main () {
struct timeval tv1, tv2;
struct timezone tz;
int iterations = 10000;
double i;
double v = 1000.0;
double y = 0.0;
// First sqrt
gettimeofday(&tv1,&tz);
for(i = 0;i<iterations;i++) {
y++;
v = sqrt(y);
}
gettimeofday(&tv2,&tz);
printf("Sqrt: %f us per sqrt\n",
((tv2.tv_sec - tv1.tv_sec) * 1000.0 * 1000.0 +
(tv2.tv_usec - tv1.tv_usec) )/iterations);
// then exponent
gettimeofday(&tv1,&tz);
for(i = 0;i<iterations;i++) {
y++;
v = pow(y,0.51);
}
gettimeofday(&tv2,&tz);
printf("Pow: %f us per sqrt\n",
((tv2.tv_sec - tv1.tv_sec) * 1000.0 * 1000.0 +
(tv2.tv_usec - tv1.tv_usec) )/iterations);
// then min
gettimeofday(&tv1,&tz);
for(i = 0;i<iterations;i++) {
y++;
v = (v < y ? v : y);
}
gettimeofday(&tv2,&tz);
printf("Min: %f us per sqrt\n",
((tv2.tv_sec - tv1.tv_sec) * 1000.0 * 1000.0 +
(tv2.tv_usec - tv1.tv_usec) )/iterations);
}
|