编译器菜鸟一枚,实验中发现如下问题:在gcc下进行乘法运算时,当其中一个乘数值解决1e-40是,乘法的性能较之其他数值(1e-30、1e-50等)下降大概10倍左右,这个数值为啥有这么大“魔力”?不解,请教各位大神。
test_float.c
-------------------------------------------------
#include <stdio.h>
#include <string.h>
float foo(float a, float b, int count) {
float tmp = 0.;
int i = 0;
for (i = 0; i < count; i ++) {
tmp += a * b;
}
return tmp;
}
int main(int argc, char** argv) {
int count = 10000000;
float a = 0.40306925773620605;
float b = 0.7346844673156738;
float c = a * 1e-30;
// float c = a * 1e-40;
// float c = a * 1e-50;
float result = foo(c, b, count);
printf("Time(1e-1 * 1e-30): %lf\n", result);
return 0;
}
----------------------------------------------------------------
编译运行结果:
1. float c = a * 1e-30;
> gcc -o a.out -O0 test_float_mul.c
> time ./a.out
> real 0m0.028s
2. float c = a * 1e-40;
> gcc -o a.out -O0 test_float_mul.c
> time ./a.out
> real 0m0.405s
3. float c = a * 1e-50;
> gcc -o a.out -O0 test_float_mul.c
> time ./a.out
> real 0m0.028s
test_float.c
-------------------------------------------------
#include <stdio.h>
#include <string.h>
float foo(float a, float b, int count) {
float tmp = 0.;
int i = 0;
for (i = 0; i < count; i ++) {
tmp += a * b;
}
return tmp;
}
int main(int argc, char** argv) {
int count = 10000000;
float a = 0.40306925773620605;
float b = 0.7346844673156738;
float c = a * 1e-30;
// float c = a * 1e-40;
// float c = a * 1e-50;
float result = foo(c, b, count);
printf("Time(1e-1 * 1e-30): %lf\n", result);
return 0;
}
----------------------------------------------------------------
编译运行结果:
1. float c = a * 1e-30;
> gcc -o a.out -O0 test_float_mul.c
> time ./a.out
> real 0m0.028s
2. float c = a * 1e-40;
> gcc -o a.out -O0 test_float_mul.c
> time ./a.out
> real 0m0.405s
3. float c = a * 1e-50;
> gcc -o a.out -O0 test_float_mul.c
> time ./a.out
> real 0m0.028s