Java性能:真实与Boolean.TRUE [英] Java performance: true vs. Boolean.TRUE

查看:3237
本文介绍了Java性能:真实与Boolean.TRUE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在性能和​​内存使用效率方面更好呢?

Which 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;
}

应该使用引用静态对象时会更快,更容易与原始类型的工作,但在另一方面,没有任何新的价值创造。或者,可能它是在编译器级别的优化,所有的真正通过静态对象的引用替换保存内存?

It should be faster and easier to work with primitive types, but in other hand when using reference to static object, no new value created. Or may be it is optimized on compiler level and all true and false are replaced by references to the static objects to save memory?

推荐答案

首先,使用比其他任何一个的性能优势的最有可能的太小是相关的。 code简单/可读性/可维护性是一个更为重要的......在绝大多数情况下。

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.

的例子没有涉及创建一个布尔实例。这在理论上是可能的4 3将触发布尔类......和您的应用程序本来不会那样做初始化。在不大可能事件,整个应用程序将分配本来不会被分配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.

这人会等于或大于所有其他的更快,因为它只是蕴涵A寄存器设置为零。

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(真)来框中的真正,但JIT编译器应该能够通过内联调用它优化到相同的code为previous之一。


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)来解包的布尔。此调用可以内联。这也有可能是JIT编译器可避免加载参考布尔对象,并获取其value字段。


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编译器可以(至少在理论上)产生相同的(最佳)code为他们所有。


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.

这篇关于Java性能:真实与Boolean.TRUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆