Java performance: true vs. Boolean.TRUE

2020-02-08 02:29发布

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

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?

9条回答
够拽才男人
2楼-- · 2020-02-08 03:09

My rules of thumb are as follows:

  1. The default choice is the primitive type (boolean).
  2. If I need nullability or need to store the values in a container, I use the class (Boolean).

With this in mind, my default choice would be:

boolean isItTrue(arg){
    return true;
}

As far as performance is concerned, the only thing that seems certain is that it's hard to imagine a scenario where using Boolean would be faster than using boolean. Whether it would be slower or the same depends on a lot of things and it's impossible to answer in general.

If you really care about this, profile the code where it matters!

查看更多
老娘就宠你
3楼-- · 2020-02-08 03:11

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.


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

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

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

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
}

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.

查看更多
混吃等死
4楼-- · 2020-02-08 03:18

They will be much faster. I don't think there will be any difference between these two.

Boolean isItTrue(arg){ 
    return Boolean.TRUE;
}

boolean isItTrue(arg){
    return true;
}

But other implementation will be slower because it will be boxing and unboxing on the back end with will take some time of processor.


Edit

I have collected some facts by implementing the 4 different ways. Just want to share it with you, I don't if its the write way to do that.

Boolean isItTrue(){ 
    return Boolean.TRUE;
}

Free Memory before start --> 16030936
Time taken in Secs --> 7.844
Free Memory After Process --> 15940472
Memory Usage --> 90464

boolean isItTrue(){
    return Boolean.TRUE;
}

Free Memory before start --> 16030936
Time taken in Secs --> 10.109
Free Memory After Process --> 15940472
Memory Usage --> 90464

Boolean isItTrue(){
    return true;
}

Free Memory before start --> 16030936
Time taken in Secs --> 7.906
Free Memory After Process --> 15940472
Memory Usage --> 90464

boolean isItTrue(){
    return true;
}

Free Memory before start --> 16030936
Time taken in Secs --> 7.828
Free Memory After Process --> 15940472
Memory Usage --> 90464

Main Class

public static void main(String[] args){
    NewClass n = new NewClass();

    long sysTime = System.currentTimeMillis();

    Runtime rt = Runtime.getRuntime();
    long freeMem = rt.freeMemory();
    System.out.println( "Free Memory before start --> " + freeMem );
    for( int i = 0; i < Integer.MAX_VALUE; i++ ){
        n.isItTrue();
    }
    System.out.println( "Time taken in Secs --> " + (System.currentTimeMillis() - sysTime)/1000D);
    System.out.println( "Free Memory After Process --> " + rt.freeMemory() );
    System.out.println( "Memory Usage --> " + ( freeMem - rt.freeMemory() ) );
}
查看更多
登录 后发表回答