x * x vs Math.pow(x,2)java性能 [英] x*x vs Math.pow(x,2) java performance
问题描述
我已经做过一些关于x * x或Math.pow(x,2)在Java中是否更快的测试。我期待简单的x * x有点快,但事实证明它的速度相当快。有人可以告诉我,请问这有什么可能吗?
I have done some testing about whether x * x or Math.pow(x,2) is faster in Java. I was expecting simple x * x to be somewhat faster, however, it turned out that its about equaly fast. Can someone enlighten me, how is that possible, please?
推荐答案
你知道它已被JIT(甚至已经编译 - 时间)同样的事情。这些微基准测试很少能提供非常有用的结果,因为没有真正的上下文。
For all you know it's JITted (or even already in compile-time) down to the same exact thing. These kinds of micro-benchmarks rarely give very usable results, since there is no real context.
这绝对不是偏爱另一个的理由,因为现实世界的代码很少有一个简单的 x ^ 2
操作作为性能热点。
It's definitely not a reason to prefer one over another, since real world code rarely has a simple x^2
operation as a performance hotspot.
这篇关于x * x vs Math.pow(x,2)java性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!