哪個性能比較好?什麼是java字節碼性能getfield vs invokevirtual
我問這個問題沒有測試,因爲我很懶。現在經過測試,它顯示getMethod稍快於點域
Integer xj = x.getJ();`
或
Integer yj = x.j;
下面是Java字節碼我沒有後反編譯
L5 {
aload1
invokevirtual testj/ByteCodeTest getJ(()Ljava/lang/Integer;);
astore4
}
L6 {
aload1
getfield testj/ByteCodeTest.j:java.lang.Integer
astore5
}
這裏我正在測試的代碼:
public void setPoint(){
point=System.currentTimeMillis();
System.out.println("point"+point);
}
public void comparePoint(){
long endPoint=System.currentTimeMillis();
System.out.println("endPoint"+endPoint);
System.out.println("inteval"+(endPoint-point));
}
int count =2000000000;
public void test22(){
ByteCodeTest x = new ByteCodeTest();
setPoint();
for(int i=0;i<count;i++){
int yy= x.i+1;
}
comparePoint();
setPoint();
for(int i=0;i<count;i++){
int yy=x.getI()+1;
}
comparePoint();
}
下面是代碼輸出:
point1490454906205
endPoint1490454907447
inteval1242
point1490454907448
endPoint1490454908666
inteval1218
這意味着getMethod比點域
爲什麼?出於性能原因你打算選擇一個嗎? – Kayaman
如果在這個分鐘級別的性能差異對您有影響,您應該使用較低級別的編程語言,而不是使用Java – ControlAltDel