Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java use getter in for loop or create a local variable? [duplicate]

I have a for loop which runs 4096 times and it should be as fast as possible. Performance is really important here. Currently I use getter methods inside the loop which just return values or objects from fields which don't change while the loop is in progress.

Example:

for (;;) {
    doSomething(example.getValue());
}

Is there any overhead using getters? Is it faster using the following way?

Example:

Object object = example.getValue();
for (;;) {
    doSomething(object);
}

If yes, is that also true for accessing public fields like example.value?

Edit: I don't use System.out.println() inside the loop.

Edit: Some fields are not final. No fields are volatile and no method (getter) is synchronized.

like image 328
stonar96 Avatar asked Apr 11 '16 18:04

stonar96


People also ask

Should you use the getter method also whenever you use the setter method?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

Can you use a variable from a for loop in Java?

NO, it wouldn't be possible as the scope of the variable declared in the loop is limited to the loop.

What is getter () used for?

Getter returns the value (accessors), it returns the value of data type int, String, double, float, etc. For the program's convenience, getter starts with the word “get” followed by the variable name. While Setter sets or updates the value (mutators). It sets the value for any variable used in a class's programs.

What can I use instead of getters and setters?

You may use lombok - to manually avoid getter and setter method. But it create by itself. The using of lombok significantly reduces a lot number of code.


2 Answers

As Rogério answered, getting the object reference outside the loop (Object object = example.getValue();) will likely be faster (or will at least never be slower) than calling the getter inside the loop because

  • in the "worst" case, example.getValue() might actually do some very computationally-expensive stuff in the background despite that getter methods are supposed to be "trivial". By assigning a reference once and re-using it, you do this expensive computation only once.
  • in the "best" case, example.getValue() does something trivial such as return value; and so assigning it inside the loop would be no more expensive than outside the loop after the JIT compiler inlines the code.

However, more important is the difference in semantics between the two and its possible effects in a multi-threaded environment: If the state of the object example changes in a way which causes example.getValue() to return references to different objects, it is possible that, in each iteration, the method doSomething(Object object) will actually operate on a different instance of Object by directly calling doSomething(example.getValue());. On the other hand, by calling a getter outside the loop and setting a reference to the returned instance (Object object = example.getValue();), doSomething(object); will operate on object n times for n iterations.

This difference in semantics can cause behavior in a multi-threaded environment to be radically different from that in a single-threaded environment. Moreover, this need not be an actual "in-memory" multi-threading issue: If example.getValue() depends on e.g. database/HDD/network resources, it is possible that this data changes during execution of the loop, making it possible that a different object is returned even if the Java application itself is single-threaded. For this reason, it is best to consider what you actually want to accomplish with your loop and to then choose the option which best reflects the intended behavior.

like image 98
errantlinguist Avatar answered Nov 15 '22 15:11

errantlinguist


It depends on the getter.

If it's a simple getter, the JIT will in-line it to a direct field access anyway, so there won't be a measurable difference. From a style point of view, use the getter - it's less code.

If the getter is accessing a volatile field, there's an extra memory access hit as the value can't be kept in the register, however the hit is very small.

If the getter is synchronized, then using a local variable will be measurably faster as locks don't need to be obtained and released every call, but the loop code will use the potentially stale value of the field at the time the getter was called.

like image 40
Bohemian Avatar answered Nov 15 '22 13:11

Bohemian