ofe*_*fey 1 c++ math floating-point loops
我需要多次计算浮点数的平方.(大约10 ^ 8)
哪个更好(因为时间是一个巨大的限制):
pdt=pow(num,2);
OR
pdt=num*num
Run Code Online (Sandbox Code Playgroud)
或者他们真的一样吗?
编辑:在合理大的输入上检查两种样式时,我的处理器给出了相反的结果.
使用没有optmisations的gcc,由于pow
发生函数调用,num*num更快.在-O2
它们输出相同的asm(对于x86):
float numpow(float x){
return pow(x, 2);
}
float mulpow(float x){
return x*x;
}
compiled with g++ -S -O2 -c
__Z6numpowf:
Leh_func_begin1:
pushq %rbp
Ltmp0:
movq %rsp, %rbp
Ltmp1:
mulss %xmm0, %xmm0
popq %rbp
ret
Leh_func_end1:
...
__Z6mulpowf:
Leh_func_begin2:
pushq %rbp
Ltmp2:
movq %rsp, %rbp
Ltmp3:
mulss %xmm0, %xmm0
popq %rbp
ret
Run Code Online (Sandbox Code Playgroud)