使用C语言写一个测试代码实现intel的remainder\remainderf\remainderl函数性能测试并且把测试数和结果输入到txt文件中
时间: 2023-08-11 13:05:03 浏览: 62
以下是使用C语言的代码,测试并记录了三种不同类型的remainder函数的性能,并将测试数和结果写入了txt文件中:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define TEST_NUM 100000
int main() {
int i;
double x, y, z;
float xf, yf, zf;
long double xl, yl, zl;
clock_t start, end;
double cpu_time_used;
FILE *fp;
char filename[100];
// get desktop path
char *desktop = getenv("USERPROFILE");
sprintf(filename, "%s\\Desktop\\result.txt", desktop);
fp = fopen(filename, "w");
// test remainder function
fprintf(fp, "Testing remainder function...\n");
fprintf(fp, "x\t\ty\t\tz\n");
start = clock();
for (i = 0; i < TEST_NUM; i++) {
x = (double) rand() / RAND_MAX * 100;
y = (double) rand() / RAND_MAX * 10;
z = remainder(x, y);
fprintf(fp, "%f\t%f\t%f\n", x, y, z);
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
fprintf(fp, "Time used: %f seconds.\n\n", cpu_time_used);
// test remainderf function
fprintf(fp, "Testing remainderf function...\n");
fprintf(fp, "x\t\ty\t\tz\n");
start = clock();
for (i = 0; i < TEST_NUM; i++) {
xf = (float) rand() / RAND_MAX * 100;
yf = (float) rand() / RAND_MAX * 10;
zf = remainderf(xf, yf);
fprintf(fp, "%f\t%f\t%f\n", xf, yf, zf);
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
fprintf(fp, "Time used: %f seconds.\n\n", cpu_time_used);
// test remainderl function
fprintf(fp, "Testing remainderl function...\n");
fprintf(fp, "x\t\ty\t\tz\n");
start = clock();
for (i = 0; i < TEST_NUM; i++) {
xl = (long double) rand() / RAND_MAX * 100;
yl = (long double) rand() / RAND_MAX * 10;
zl = remainderl(xl, yl);
fprintf(fp, "%Lf\t%Lf\t%Lf\n", xl, yl, zl);
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
fprintf(fp, "Time used: %f seconds.\n\n", cpu_time_used);
fclose(fp);
return 0;
}
```
代码中使用了C标准库中的rand()函数生成了100,000个随机数进行测试,并将测试结果写入了文件。可以根据需要修改TEST_NUM的值来改变测试数的数量。同时,代码中也可以根据需要修改文件名和路径。