which performance is better?
I asked this question without testing because i am lazy. Now after testing, it shows getMethod is slightly faster than .field
Integer xj = x.getJ();`
or
Integer yj = x.j;
Here is java byte code after i did de-compile
L5 {
aload1
invokevirtual testj/ByteCodeTest getJ(()Ljava/lang/Integer;);
astore4
}
L6 {
aload1
getfield testj/ByteCodeTest.j:java.lang.Integer
astore5
}
Here is the code i am testing:
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();
}
Here is the code output:
point1490454906205
endPoint1490454907447
inteval1242
point1490454907448
endPoint1490454908666
inteval1218
It means getMethod is slightly faster than .field