我在使用小精度浮点数执行算术运算时检测到了不寻常的计算时间.以下简单代码表现出此行为:
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
const int MAX_ITER = 100000000;
int main(int argc, char *argv[]){
double x = 1.0, y;
int i;
clock_t t1, t2;
scanf("%lf", &y);
t1 = clock();
for (i = 0; i < MAX_ITER; i++)
x *= y;
t2 = clock();
printf("x = %lf\n", x);
printf("Time: %.5lfsegs\n", ((double) (t2 - t1)) / CLOCKS_PER_SEC);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下是该程序的两个不同运行:
y = 0.5
x = 0.000000
时间:1.32000秒
y = 0.9
x = 0.000000
时间:19.99000秒
我正在使用具有以下规格的笔记本电脑来测试代码: