pow的效率(num,2)?

ofe*_*fey 1 c++ math floating-point loops

我需要多次计算浮点数的平方.(大约10 ^ 8)

哪个更好(因为时间是一个巨大的限制):

 pdt=pow(num,2);

      OR

 pdt=num*num
Run Code Online (Sandbox Code Playgroud)

或者他们真的一样吗?

编辑:在合理大的输入上检查两种样式时,我的处理器给出了相反的结果.

Eri*_*hil 10

num*num将至少与pow(num, 2)任何非反常的C/C++实现一样快,因为没有pow至少一个浮点乘法或更耗时的操作就没有实现.


cmh*_*cmh 6

使用没有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)