#include <sys/time.h>
#include <stdio.h>
double getdoubletime() {
struct timeval t;
gettimeofday(&t, NULL);
return (double) t.tv_sec + ((double) t.tv_usec * 1e-6);
}
#include <math.h>
#include <stdlib.h>
#define N 65536
int main(int argc, char const *argv[]) {
size_t i;
double t0;
// generate some random floating-point numbers
double numbers[N];
double fractionals[N];
for (i=0; i<N; ++i) {
numbers
[i
] = rand() + rand() / (double)RAND_MAX
; }
// extract fractional part: method 1
t0 = getdoubletime();
for (i=0; i<N; ++i) {
fractionals[i] = remainder(numbers[i], 1.0);
}
printf("method 1 took %lf seconds (using remainder)\n", getdoubletime
() - t0
); // extract fractional part: method 2
t0 = getdoubletime();
for (i=0; i<N; ++i) {
fractionals[i] = numbers[i] - (long)numbers[i];
}
printf("method 2 took %lf seconds (casting to long)\n", getdoubletime
() - t0
); // extract fractional part: method 3
t0 = getdoubletime();
for (i=0; i<N; ++i) {
fractionals
[i
] = numbers
[i
] - floor(numbers
[i
]); }
printf("method 3 took %lf seconds (using floor)\n", getdoubletime
() - t0
); // extract fractional part: method 4
t0 = getdoubletime();
double integral;
for (i=0; i<N; ++i) {
fractionals
[i
] = modf(numbers
[i
], &integral
); }
printf("method 4 took %lf seconds (using modf)\n", getdoubletime
() - t0
); // extract fractional part: method 5
t0 = getdoubletime();
for (i=0; i<N; ++i) {
fractionals[i] = fmod(numbers[i], 1.0);
}
printf("method 5 took %lf seconds (using fmod)\n", getdoubletime
() - t0
);
return 0;
}
I2luY2x1ZGUgPHN5cy90aW1lLmg+CiNpbmNsdWRlIDxzdGRpby5oPgoKZG91YmxlIGdldGRvdWJsZXRpbWUoKSB7CiAgICBzdHJ1Y3QgdGltZXZhbCB0OwogICAgZ2V0dGltZW9mZGF5KCZ0LCBOVUxMKTsKICAgIHJldHVybiAoZG91YmxlKSB0LnR2X3NlYyArICgoZG91YmxlKSB0LnR2X3VzZWMgKiAxZS02KTsKfQoKI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHN0ZGxpYi5o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