1

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

user3073309
  • 184
  • 2
  • 5
  • 1
    Why? Do you intend to choose one over the other for performance reasons? – Kayaman Mar 24 '17 at 17:32
  • 2
    If performance differences at this minute level matter to you, you should probably be using a lower level programming language rather than using Java – ControlAltDel Mar 24 '17 at 17:35

3 Answers3

6

Field access is faster than a method call, but unless you're writing extremely performance sensitive code in a tight loop, the decision to use one over the other is design, not performance.

Add JIT to the mix and you might not be getting any advantage from direct field access.

Your testing shows that method call is faster, but that's because you're not benchmarking correctly. Performance is a complicated matter, and beginners (and even not so beginners) don't usually have enough experience to take everything into account. They don't know how to measure performance, they worry about irrelevant micro-optimization which doesn't affect performance at all (in most software), they aren't well aware of JIT or other things that are in play, and many many other issues that makes performance and inexperienced developers a dangerous mix.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
4

The bytecode is different, but in the end, the JIT will optimize the access by inlining the getter. Unlike the programmer at the time writing the code, the JIT can accurately tell at any time if the getter is overwritten or not. Trust the JIT on this one, the performance is identical in the long run.

Whats not identical, is the maintainability. Using a getter method allows you to overwrite the getter with extended/modified logic (The price for doing so will be making the getter polymorphic, inducing a small performance penalty, on the other hand this would not even be possible with direct field access).

Using the direct field access gets you no advantages, but a lot of pitfalls when the code evolves later on.

Durandal
  • 19,919
  • 4
  • 36
  • 70
0

When you use a getter, you call a function which reduces the performance rather than just accessing a field. If you look at your byte code in the first case you should find a definition for getJ which is called here.

Peyman Mahdian
  • 522
  • 4
  • 15