ian*_*sme 2 java micro-optimization
我很想知道这两个Java方法调用中的任何一个在处理器时间,内存分配和/或垃圾收集方面是否会表现得完全不同.
SomeObject myObj = new SomeObject();
myObj.doSomething();
Run Code Online (Sandbox Code Playgroud)
与
new SomeObject().doSomething();
Run Code Online (Sandbox Code Playgroud)
查看生成的字节码:
// 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
Run Code Online (Sandbox Code Playgroud)
你可以清楚地看到这个有另外两个指令:
// 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
Run Code Online (Sandbox Code Playgroud)
这些指令似乎非常冗余且易于优化.我敢打赌JIT编译器会在需要时处理它们.
| 归档时间: |
|
| 查看次数: |
177 次 |
| 最近记录: |