就性能而言,隐式(自动)拆箱和显式拆箱哪个更好?

发布于 2024-11-27 16:25:52 字数 671 浏览 3 评论 0原文

将其放入代码中 - 哪个具有更好的性能(如果有差异的话)?

鉴于此:

public class Customer
{
    ....

    public Boolean isVIP(){...}
    ...
}

哪个更快?

public void handleCustomer(Customer customer)
{
    if (customer.isVIP())  // Auto Unboxing
    {
        handleNow(customer);
    }
    else
    {  
        sayHandlingNowButQueueForTomorrow(customer);
    }
}

或者这个:

public void handleCustomer(Customer customer)
{
    if (customer.isVIP().booleanValue()) // Explicit unboxing
    {
        handleNow(customer);
    }
    else
    {  
        sayHandlingNowButQueueForTomorrow(customer);
    }
}

To put it in code - which has better performance (if there is a difference at all)?

Given this:

public class Customer
{
    ....

    public Boolean isVIP(){...}
    ...
}

Which is faster?

public void handleCustomer(Customer customer)
{
    if (customer.isVIP())  // Auto Unboxing
    {
        handleNow(customer);
    }
    else
    {  
        sayHandlingNowButQueueForTomorrow(customer);
    }
}

or this:

public void handleCustomer(Customer customer)
{
    if (customer.isVIP().booleanValue()) // Explicit unboxing
    {
        handleNow(customer);
    }
    else
    {  
        sayHandlingNowButQueueForTomorrow(customer);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离旧人 2024-12-04 16:25:52

它们之间没有区别,你可以在字节码中验证它:

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

运行 javap 查看其编译结果:

javap -c ImplicitTest

这是输出:

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

}

如您所见 - 第 5,6,9 行(隐式)与第 10、11 行相同, 14(明确)。

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).

め七分饶幸 2024-12-04 16:25:52

差异应该全部出现在编译时,因为自动拆箱只是语法糖。在这种情况下,生成的 Java 字节码应该完全相同。这意味着运行时没有区别。但是,在更一般的情况下,显式拆箱可能会更快,因为隐式拆箱可能会多次拆箱该值,而使用显式拆箱,您可以保证该值仅拆箱一次并存储结果。

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.

逐鹿 2024-12-04 16:25:52

就性能而言,理想情况下它们应该是相同的。

人工编写的技术可能不太理想,因此如果您采用较差的人工编写的自动装箱方法,可能会影响性能。但是,如果您真的想实现它,那么人类有可能编写某种超越默认性能的非通用解决方案。这样的解决方案不会那么灵活,并且它可能会为了内存而牺牲计算复杂性(就像一个大的查找数组)。

就我个人而言,我建议花一些时间来真正了解更大的情况。优化一两行代码几乎从来都不是一项好的投资。减少整个计划中所需的工作量更有可能提高您的绩效。

请注意,在一般情况下,JVM 不会因自动装箱的引入而发生变化,只是编译器发生了变化。因此,编译器添加的指令与您在最常见情况下手动编写的指令相同。性能是在运行时在 JVM 中测量的,如果两种方式都是相同的字节码,则没有理由期望性能差异。

这有点过早优化的味道,但如果您认为可以及时发现差异:请通过仔细测试来做到这一点,然后意识到它在不同的版本、操作系统等上可能会有所不同。这并不是一个明显的胜利无论如何。

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.

花开柳相依 2024-12-04 16:25:52

VIP 是否必须返回 Boolean 而不是 boolean

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文