[ACCEPTED]-Java: Getter and setter faster than direct access?-getter-setter

Accepted answer
Score: 42

I did done some micro benchmarking with 4 lots of JVM warm up and found the two approaches take the exact same amount of execution time.

This happens 3 because the JIT compiler is in-lining the 2 getter method with a direct access to the 1 field thus making them identical bytecode.

Score: 23

Thank you all for helping me answering this 21 question. In the end, I found the answer.

First, Bohemian is right: With 20 PrintAssembly I checked the assumption that the generated 19 assembly codes are identical. And yes, although 18 the bytecodes are different, the generated 17 codes are identical.
So masterxilo is right: The profiler have 16 to be the culprit. But masterxilo's guess 15 about timing fences and more instrumentation 14 code can't be true; both codes are identical 13 in the end.

So there's still the question: How 12 is it possible that the second code seems 11 to be 6% faster in the profiler?

The answer 10 lies in the way how VisualVM measures: Before you start profiling, you need calibration data. This 9 is used for removing the overhead time caused 8 by the profiler.
Although the calibration 7 data is correct, the final calculation of 6 the measurement is not. VisualVM sees the 5 method invocations in the bytecode. But 4 it doesn't see that the JIT compiler removes 3 these invocations while optimizing.
So it 2 removes non-existing overhead time. And 1 that's how the difference appears.

Score: 0

In case you have not taken a course in Statistics, there 43 is always variance in program performance no matter 42 how well that it is written. The reason 41 why these two methods seem to run at approximately 40 the same rate is because the accessor fields 39 only do one thing: They return a particular 38 field. Because nothing else happens in the 37 accessor method, both tactics pretty much 36 do the same thing; however, in case you 35 know not about encapsulation, which is how 34 well that a programmer hides the data (fields 33 or attributes) from the user, a major rule 32 of encapsulation is not to reveal internal data to the user. Modifying a field 31 as public means that any other class can 30 access those fields, and that can be very dangerous 29 to the user. That is why I always recommend Java 28 programmers to use accessor and mutator 27 methods so that the fields will not get 26 into the wrong hands.

In case you were curious 25 about how to access a private field, you 24 can use reflection, which actually accesses 23 the data of a particular class so that you 22 can mutate it if you really must do so. As 21 a frivolous example, suppose that you knew 20 that the java.lang.String class contains 19 a private field of type char[] (that is, a 18 char array). It is hidden from the user, so 17 you cannot access the field directly. (By 16 the way, the method java.lang.String.toCharArray() accesses 15 the field for you.) If you wanted to access 14 each character consecutively and store each 13 character into a collection (for the sake 12 of simplicity, why not a java.util.List?), then 11 here is how to use reflection in this case:

    This method iterates through each character in a <code>String</code> and places each of them into a <code>java.util.List</code> of type <code>Character</code>.
    @param str The <code>String</code> to extract from.
    @param list The list to store each character into. (This is necessary because the compiler knows not which <code>List</code> to use, so it will automatically clear the list anyway.)
public static void extractStringData(String str, List<Character> list) throws IllegalAccessException, NoSuchFieldException
    java.lang.reflect.Field value = String.class.getDeclaredField("value");
    char[] data = (char[]) value.get(str);
    for(char ch : data) list.add(ch);

As 10 a sidenote, note that reflection takes a 9 lot of performance out of your program. If 8 there is a field, method, or inner or nested 7 class that you must access for whatever reason 6 (which is highly unlikely anyway), then 5 you should use reflection. The main reason 4 why reflection takes away precious performance 3 is because of the relatively innumerable 2 exceptions that it throws. I am glad to 1 have helped!

More Related questions