Which is better in terms of performance, implicit (auto) unboxing or explicit unboxing?

穿精又带淫゛_ 提交于 2019-11-28 11:50:13
Mark D

No difference between them, you can verify it in the bytecode:

public class ImplicitTest {
    public static void main(String[] args) {
        Boolean b = true; 
        boolean i = b;
        boolean e = b.booleanValue();
    }
}

Run javap to see what it compiles to:

javap -c ImplicitTest

Here is the output:

Compiled from "ImplicitTest.java"
public class ImplicitTest extends java.lang.Object{
public ImplicitTest();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   iconst_1
   1:   invokestatic    #2; //Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
   4:   astore_1
   5:   aload_1
   6:   invokevirtual   #3; //Method java/lang/Boolean.booleanValue:()Z
   9:   istore_2
   10:  aload_1
   11:  invokevirtual   #3; //Method java/lang/Boolean.booleanValue:()Z
   14:  istore_3
   15:  return

}

As you can see - lines 5,6,9 (implicit) are the same as 10, 11, 14 (explicit).

The difference should all be at compile time, since auto unboxing is just syntactic sugar. In this case the Java bytecode generated should be exactly the same. This means no difference at runtime. However, in the more general case explicit unboxing may be faster, because implicit unboxing may unbox the value more than once, while with explicit unboxing you can guarantee that the value is unboxed only once and the result stored.

Performance wise, they ideally should be the same.

There's a chance that the human written techniques are a bit less optimal, so it might be a performance hit if you do poor human written methods of autoboxing. But, if you really want to get to it, there's an equal chance that a human might write some sort of non-general solution which beats default performance. Such a solution would not be as flexible, and it would probably trade off computational complexity for memory (like a big lookup array).

Personally, I'd recommend taking some time to really view the larger picture. Optimizing a single line or two of code is almost never a good investment. Reducing the amount of work necessary in the entire program is more likely to get you your performance boosts.

Note that in the general case, the JVM didn't change with the introduction of autoboxing, just the compiler did. So the compiler is adding the same instructions as you would write out manually in the most common cases. The performance is measure in the JVM at runtime, and if it's the same bytecodes either way, there's no reason to expect a performance difference.

This just smacks of premature optimization, but if you think you can find a difference in time: do so with careful testing, and then realize that it may be different on different point releases, operating systems, etc. It's just not a clear cut win in any case.

Is a VIP so VI that it must return Boolean instead of boolean?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!