Pow() vs. exp() performance

前端 未结 3 1333
耶瑟儿~
耶瑟儿~ 2021-02-19 09:33

I was wondering if exp() is faster than more general pow(). I run fast benchmark on JsPerf http://jsperf.com/pow-vs-exp and it shown interesting result

3条回答
  •  -上瘾入骨i
    2021-02-19 10:30

    As a partial answer, there are instructions for exp, log or pow on some architectures yes. However, that doesn't necessarily mean much.

    For example, on x86 there's

    • f2xm1 which calculates 2x - 1
    • fscale which calculates y * 2(int)x
    • fyl2x which calculates y * log2 x
    • fyl2xp1 which calculates y * log2(x + 1) (has restrictions on input range)

    However, they are not much used. It varies from architecture to architecture, but they're never fast. As a more extreme example, fyl2x has a latency of 724 on Sandy Bridge (pretty recent!), in that time on the same processor you could do about 700 independent floating point additions, or about 240 dependent floating point additions, or about 2000 independent simple integer operations.

    That's about as bad as it gets, but they're typically slow. Slow enough that a manual implementation can beat them or at least not significantly lose.

    Also, FPU code is slowly disappearing in favour of SSE code. There are no SSE equivalents of those instructions.

提交回复
热议问题