are 2^n exponent calculations really less efficient than bit-shifts?

后端 未结 4 960
长情又很酷
长情又很酷 2020-12-15 21:01

if I do:

int x = 4;
pow(2, x);

Is that really that much less efficient than just doing:

1 << 4

?

4条回答
  •  庸人自扰
    2020-12-15 21:44

    That depends on the compiler, but in general (when the compiler is not totally braindead) yes, the shift is one CPU instruction, the other is a function call, that involves saving the current state an setting up a stack frame, that requires many instructions.

提交回复
热议问题