Tomáš Mocek - 8 months ago 31

Java Question

I am trying to compare performance of

`pow(x,2.0)`

`pow(x,2.0000001)`

`2.0`

`-Xint`

Any idea why is that, please?

Thanks a lot!

Answer

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
```