Math.max(a,b) 还是 (a>b)?a:b 在 Java 中更快?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2103606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Math.max(a,b) or (a>b)?a:b faster in Java?
提问by giri
Which one is faster in Java and why?
哪一个在 Java 中更快,为什么?
Math.max(a,b)(a>b)?a:b
Math.max(a,b)(a>b)?a:b
(This was asked in an interview.)
(这是在一次采访中被问到的。)
采纳答案by dsimcha
Math.max(a, b)is a static function (meaning no virtual call overhead) and will likely be inlined by the JVM to the same instructions as (a > b) ? a : b.
Math.max(a, b)是一个静态函数(意味着没有虚拟调用开销)并且很可能被 JVM 内联到与(a > b) ? a : b.
回答by jjnguy
Hereis the openjdkcode for Math.max()in Java:
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
So, the code would probably be (almost) exactly the same speed.
因此,代码可能(几乎)完全相同的速度。
(Lets be honest, if you are worrying about speed improvements at such a low level, you probably have far greater problems in your code.)
(老实说,如果您担心如此低级别的速度改进,那么您的代码中可能存在更大的问题。)
回答by x4u
Performance questions always call for a test before you can start speculating:
在开始推测之前,性能问题总是需要测试:
public static void maxtest()
{
int res = 0;
for( int idx = 0; --idx != 0; )
// res = ( res > idx ) ? res : idx;
res = Math.max( res, idx );
System.out.println( "res: " + res );
}
This runs on my machine 6 seconds with Math.max()and 3.2 seconds with ?:on the latest 1.6.1 x64 server Sun JVM. So ?:is actually faster. Contrary to all the hopes we like to put in the JITs that have really become amazing by the time they still don't catch everything.
这在我的机器上运行 6 秒,在最新的 1.6.1 x64 服务器 Sun JVM 上运行Math.max()3.2 秒?:。所以?:实际上更快。与我们对 JIT 抱有的所有希望相反,当 JIT 仍然无法捕获所有内容时,它们确实变得非常棒。
EDIT: Out of curiosity I also tried this code with the 32 bit client JVM 1.6.1 on the same machine and with this both versions run in 7 seconds! So it's probably not the method invocation that doesn't get inlined but the server JIT seems to be able to do some additional optimizations for this particular test case that it can't detect when there is a method call involved.
编辑:出于好奇,我还在同一台机器上用 32 位客户端 JVM 1.6.1 尝试了这段代码,这两个版本都在 7 秒内运行!因此,可能不是未内联的方法调用,而是服务器 JIT 似乎能够为这个特定的测试用例做一些额外的优化,它无法检测到何时涉及方法调用。
回答by Has QUIT--Anony-Mousse
Do not rely on speculation. Instead, benchmarkyour particular use case.
不要依赖投机。相反,对您的特定用例进行基准测试。
Some easily overlooked details in many of the other answers:
许多其他答案中的一些容易被忽视的细节:
While you can see a Java source of Math.max, this is actually not alwayswhat will be used. This method has an intrinsicversion in pretty much every JRE. See the source code of Hotspot in JDK7, vmSymbols.hppfor a list of such intrinsics.
虽然您可以看到 的 Java 源代码Math.max,但这实际上并不总是会被使用。这种方法在几乎每个 JRE 中都有一个内在版本。有关此类内在函数的列表,请参阅JDK7 中 HotspotvmSymbols.hpp的源代码。
As far as I can tell, Hotspot will try a number of optimizations when it sees a maxor minstatement; in particular to optimize e.g. arraycopy. Amongst others, it will actually optimize Math.max(same, same)away.
据我所知,Hotspot 在看到maxormin语句时会尝试一些优化;特别是优化例如arraycopy。除其他外,它实际上会优化Math.max(same, same)掉。
In other cases, however, it may not optimize much; (a<=b)?a:bmay then actually be faster. I've been benchmarking a bit, and indeed I often found this to be faster. But YMMV, and it definitely depends on the context if Hotspot can optimize one better or the other. It will also vary from hotspot version to hotspot version...
然而,在其他情况下,它可能不会优化太多;(a<=b)?a:b那么实际上可能会更快。我一直在进行基准测试,实际上我经常发现这更快。但是 YMMV,如果 Hotspot 可以更好地优化一个或另一个,这绝对取决于上下文。它也会因热点版本而异...
回答by jarnbjo
If I had asked such a question in an interview, I would have expected the candidate to tell me that the two expressions may not give the same result for all possible types of a and b.
如果我在面试中问过这样的问题,我会期望应聘者告诉我,对于所有可能的 a 和 b 类型,这两个表达式可能不会给出相同的结果。
回答by Mark Thornton
The original question doesn't specify the type of the arguments. This matters because the definition of max (and min) for floating point arguments is more complex. For floating point (double or float) the Math.max method is likely to be slower, but it also may return a different result if one of the arguments is NaN.
原始问题没有指定参数的类型。这很重要,因为浮点参数的最大值(和最小值)的定义更为复杂。对于浮点数(double 或 float),Math.max 方法可能会更慢,但如果参数之一是 NaN,它也可能返回不同的结果。
回答by XMLDUDE
Not the same. When you are writing (a > b) ? a : byou don't have an extra function call, so it will be faster. It's the equivalent of inlining in C++.
But this will not make any difference in real life. Math.max(a,b)is more readable so I would use it.
不一样。编写(a > b) ? a : b时没有额外的函数调用,因此速度会更快。它相当于 C++ 中的内联。但这不会对现实生活产生任何影响。Math.max(a,b)更具可读性,所以我会使用它。

