Floating point division vs floating point multiplication

后端 未结 7 1771
醉话见心
醉话见心 2020-11-22 11:37

Is there any (non-microoptimization) performance gain by coding

float f1 = 200f / 2

in comparision to

float f2 = 200f * 0.5         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 12:01

    Yes, many CPUs can perform multiplication in 1 or 2 clock cycles but division always takes longer (although FP division is sometimes faster than integer division).

    If you look at this answer you will see that division can exceed 24 cycles.

    Why does division take so much longer than multiplication? If you remember back to grade school, you may recall that multiplication can essentially be performed with many simultaneous additions. Division requires iterative subtraction that cannot be performed simultaneously so it takes longer. In fact, some FP units speed up division by performing a reciprocal approximation and multiplying by that. It isn't quite as accurate but is somewhat faster.

提交回复
热议问题