Java 性能:true 与 Boolean.TRUE
以下哪项在性能和内存使用效率方面更好?
Which of the following is better in terms of performance and efficient memory usage?
Boolean isItTrue(arg){
return Boolean.TRUE;
}
boolean isItTrue(arg){
return Boolean.TRUE
}
Boolean isItTrue(arg){
return true;
}
boolean isItTrue(arg){
return true;
}
使用基本类型应该更快更容易,但另一方面,当使用对静态对象的引用时,不会创建新值.还是在编译器级别进行了优化,所有 true
和 false
都被替换为对静态对象的引用以节省内存?
It should be faster and easier to work with primitive types, but on the other hand, when using a reference to a static object, no new value is created. Or is it optimized on compiler level and all true
and false
are replaced by references to the static objects to save memory?
推荐答案
首先,使用任何一个比其他任何一个的性能优势很可能太小而无法相关.在绝大多数情况下,代码的简单性/可读性/可维护性要重要得多.
Firstly, the performance advantage of using any one over the others is most likely to be too small to be relevant. Code simplicity / readability / maintainability is a far more important ... in the vast majority of cases.
没有一个示例涉及创建 Boolean
实例.从理论上讲,这 4 个中的 3 个可能会触发 Boolean
类的初始化......并且您的应用程序不会这样做.在那种极不可能事件中,您的整个应用程序将分配 2 个原本不会被分配的对象.
None of the examples involve creating an Boolean
instances. It is theoretically possible that 3 of the 4 will trigger initialization of the Boolean
class ... and that your application wouldn't otherwise have done that. In that highly unlikely event, your entire application will allocate 2 objects that wouldn't otherwise have been allocated.
这将等于或比所有其他的更快,因为它只需将寄存器设置为零.
This one will be equal to or faster than all of the others because it simply entails setting a register to zero.
boolean isItTrue(arg){
return true;
}
<小时>
单独来看,这必须从内存中加载静态引用,而不是将寄存器归零.但是,在某些情况下,JIT 编译器可能会对此进行优化.
Taken in isolation, this has to load a static reference from memory, rather than zero a register. However, the JIT compiler may be able to optimize this away in some circumstances.
Boolean isItTrue(arg){
return Boolean.TRUE;
}
<小时>
从表面上看,这涉及到调用 Boolean.valueOf(true)
来装箱"true
,但 JIT 编译器应该能够优化通过内联调用将其转换为与前一个相同的代码.
On the face of it, this involve a call to Boolean.valueOf(true)
to "box" the true
, but the JIT compiler should be able to optimize it to the same code as the previous one by inlining the call.
Boolean isItTrue(arg){
return true;
}
<小时>
从表面上看,这涉及到调用 Boolean.booleanValue(Boolean.TRUE)
以取消装箱"Boolean
.此调用可以内联.JIT 编译器也有可能避免加载对 Boolean
对象的引用并获取其值字段.
On the face of it, this involves a call to Boolean.booleanValue(Boolean.TRUE)
to "unbox" the Boolean
. This call can be inlined. It is also possible that the JIT compiler can avoid loading the reference to the Boolean
object and fetching its value field.
boolean isItTrue(arg){
return Boolean.TRUE
}
<小时>
底线是 4 种替代方案的相对性能取决于 JIT 编译器在优化方面的成功程度.这将取决于上下文、JIT 编译器的具体情况、JVM 设置等.在最好的情况下,JIT 编译器可以(至少在理论上)为所有这些生成相同的(最佳)代码.
Bottom line is that it the relative performance of the 4 alternatives depends on how successful the JIT compiler will be in optimizing. That will depend on the context, the specific of the JIT compiler, the JVM settings, and so on. In the best case, the JIT compiler could (at least in theory) produce the same (optimal) code for all of them.
相关文章