Pow() vs. exp() performance

前端 未结 3 1329
耶瑟儿~
耶瑟儿~ 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条回答
  •  旧巷少年郎
    2021-02-19 10:13

    Yes, exp will be faster than pow in general.

    The exp and log functions will be optimized for the target platform; many techniques can be used such as Pade approximation, linear or binary reduction followed by approximation, etc.

    The pow function will generally be implemented as exp(log(a) * b) as you say, so it is obviously slower than exp alone. There are many special cases for pow such as negative exponents, integral exponents, exponents equal to 1/2 or 1/3, etc. These will slow down pow even further in the general case because these tests are expensive.

    See this SO question on pow.

提交回复
热议问题