这两个Java段的性能有什么区别吗?

发布于 2024-09-12 05:28:58 字数 214 浏览 5 评论 0原文

我很想知道这两个 Java 方法调用中的任何一个在处理器时间、内存分配和/或垃圾收集方面的行为是否会有所不同。

SomeObject myObj = new SomeObject();
myObj.doSomething();

new SomeObject().doSomething();

I'm curious to know if either of these two Java method invocations will behave differently at all in terms of processor time, memory allocation and/or garbage collection.

SomeObject myObj = new SomeObject();
myObj.doSomething();

vs.

new SomeObject().doSomething();

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

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

发布评论

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

评论(7

不喜欢何必死缠烂打 2024-09-19 05:28:58

查看生成的字节码:

// code 1
new SomeObject().doSomething();

// bytecode 1
   0:   new #2; //class SomeObject
   3:   dup
   4:   invokespecial   #3; //Method SomeObject."<init>":()V
   7:   invokevirtual   #4; //Method SomeObject.doSomething:()V
   10:  return

您可以清楚地看到这一条多了两条指令:

// code 2
SomeObject myObj = new SomeObject();
myObj.doSomething();

// bytecode 2
   0:   new #2; //class SomeObject
   3:   dup
   4:   invokespecial   #3; //Method SomeObject."<init>":()V
   7:   astore_1
   8:   aload_1
   9:   invokevirtual   #4; //Method SomeObject.doSomething:()V
   12:  return

这些指令看起来非常冗余且易于优化。我敢打赌 JIT 编译器会在需要时处理它们。

Looking at the generated bytecode:

// code 1
new SomeObject().doSomething();

// bytecode 1
   0:   new #2; //class SomeObject
   3:   dup
   4:   invokespecial   #3; //Method SomeObject."<init>":()V
   7:   invokevirtual   #4; //Method SomeObject.doSomething:()V
   10:  return

You can clearly see that this one has two more instructions:

// code 2
SomeObject myObj = new SomeObject();
myObj.doSomething();

// bytecode 2
   0:   new #2; //class SomeObject
   3:   dup
   4:   invokespecial   #3; //Method SomeObject."<init>":()V
   7:   astore_1
   8:   aload_1
   9:   invokevirtual   #4; //Method SomeObject.doSomething:()V
   12:  return

Those instructions seem very redundant and easy to optimize-out. I'd bet the JIT compiler would handle them if needed.

游魂 2024-09-19 05:28:58

差异恰好是 2 个 JVM 字节码,它转换为 1 个额外的机器指令,JIT 可能会优化掉这些指令(如果您不对变量执行任何其他操作)。

The difference is exactly 2 JVM bytecodes, which translates to 1 extra machine instruction, which the JIT may optimize away (if you don't do anything else with the variable).

提赋 2024-09-19 05:28:58

编号。    

No.    

苯莒 2024-09-19 05:28:58

假设 myObj 没有用于其他任何用途,正如我从您问题的性质推断的那样,您应该看不到任何区别。无论哪种方式,您唯一应该担心这样的开销的情况是,如果这段代码位于某个循环中,并且一遍又一遍地执行它,令人作呕。如果是这种情况,Java 的 JIT 优化应该会很好地照顾您,您应该看不到任何差异。我更喜欢看到这段代码按照第二个示例的方式编写,但这只是我的想法。

Assuming that myObj doesn't get used for anything else, as I would infer from the nature of your question, you should see no difference. Either way, the only time you should worry about overhead like this would be if this code is in some loop that is executing it over and over, ad nauseum. If that is the case, Java's JIT optimization should take very good care of you and you should see no difference. I prefer to see this code written the way your second example is, but that's just me.

楠木可依 2024-09-19 05:28:58

虽然生成的字节码可能不同,但我认为这应该是 jit 编译器更容易优化的事情之一。根据它实际的作用,甚至对象创建也可以完全优化掉。

While the generated bytecode may be different, I think that this should be one of the easier things for a jit-compiler to optimize. Depending on what it actually does, even the object creation could be optimized away altogether.

机场等船 2024-09-19 05:28:58

两者之间的唯一区别是,只要myObj引用没有被清除,并且是具有root路径的对象的一部分,分配的对象就不会被垃圾回收。

The only difference between the two is that as long as the myObj reference is not cleared and part of an object which has a path to root the allocated object will not be garbage collected.

水溶 2024-09-19 05:28:58

第一个例子的效率会稍微低一些。在第一个示例中,对象引用将一直保留到函数或封闭块结束为止。在第二个示例中,一旦调用完成,对象引用将可用于垃圾回收。当然,它实际上可能没有被垃圾收集。

我还没有检查生成的字节码。 Java 保存引用的方式可能有所不同,但无论哪种情况,我怀疑它都是微不足道的。

The first example will be slightly less efficient. In the first example, the object reference will be held until the end of the function or enclosing block. In the second example, the object reference will be available for garbage collection once the call is complete. Of course, it may not actually be garbage collected.

I haven't checked the generated bytecode. There's probably a difference in how Java holds the reference, but in either case I suspect it's trivial.

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