Java Math.pow(x,2.0) vs Math.pow(x,2.0000001) performance

后端 未结 1 835
故里飘歌
故里飘歌 2020-12-18 04:31

I am trying to compare performance of pow(x,2.0) and pow(x,2.0000001) and I though that 2.0 would be much faster, but they are at the

相关标签:
1条回答
  • 2020-12-18 04:47

    In spite of unfair downvotes, the question makes much sense, since it reveals the real JVM bug.

    When you run Oracle JDK the performance of Math.pow(x, 2.0) highly varies between JVM versions.

    • Before JDK 7u40 Math.pow used software implementation, i.e. it simply called __ieee754_pow function that emulates the operation in software. It was rather slow, but it did have a special case for y == 2.
    • Since JDK 7u40 Math.pow became a JVM intrinsic that was translated into FPU instructions by the JIT. However, with this optimization the special case has been lost, resulting in a performance regression for y == 2, see bug JDK-8029302.
    • This performance regression has been fixed in JDK 8u25 and upcoming 7u80. Since JDK 8u25 Math.pow works fast enough for all values, but extremely fast for y == 2. See the related question.

    P.S. The approximate times in seconds for 100M invocations of Math.pow on my machine with different versions of JDK.

                 Math.pow(x, 2.0)    Math.pow(x, 2.0000001)
    JDK 7u25            3.0                30.4
    JDK 7u40           11.1                11.1
    JDK 8u40            0.1                11.1      
    
    0 讨论(0)
提交回复
热议问题