FooBar
FooBar

Reputation: 1793

Java - Is calling the get method of an object slower than using a local object?

I have a variable that I'm using many times in a class and as speed is important I would like to use the fastest method.

Is there a difference in speed between using:

modifyValue(User.getSession().getSessionValue());

and setting the info to a local value such as

private String sessionValue;
sessionValue = User.getSession().getSessionValue();
modifyValue(sessionValue);

when using the variable in several different places? Which is better style, or clearer to understand? If the above example is currently being used in existing code is it worth the time to refactor?

Upvotes: 2

Views: 168

Answers (6)

Jesper
Jesper

Reputation: 206796

Performance optimization for such things can really only be done accurately based on measurements, not by reasoning about the code.

You should be asking yourself if it's really worth it to spend time trying to micro-optimize statements. A much better approach would be to first write code that works, and then use a profiler to determine what the performance bottlenecks are. Then focus on improving those. Trying to micro-optimize statements almost never gains you anything. So I suggest you stop worrying about micro-optimizations. Base optimization efforts on places that a profiler indicates are a bottleneck.

Oracle's JVM and JIT do a lot of sophisticated optimization at runtime. For example, method calls to simple getter methods are most likely inlined, so that a method call isn't even necessary. There is most likely no difference at all between the two pieces of code in your question, those will be optimized by the JIT to probably exactly the same code.

Upvotes: 2

rsp
rsp

Reputation: 23373

For most uses the two are equal with regard to speed.

With regard to maintainability I prefer to store the value in a local (final) variable if the method is not trivial.

With regard to data safety, given your description, I would advice to store it in a local variable as the code cannot be sure that the intermediate method will never result a null value, and you don't want to the return value of getSession() everywhere.

Upvotes: 0

Stephan
Stephan

Reputation: 4443

No, it is not slower. The Compiler and the just-in-time compiler of the JVM do optimizations so it should be the same. But it is a question of code quality/readability. If you write big one-liners with several method invocations, the code gets hard to read.

Upvotes: 1

AlexR
AlexR

Reputation: 115328

Each method call takes time. So, putting reference to object to member field decreases number of operations that happen when you invoke method and therefore theoretically increase the speed.

BUT: thinking about such kind of optimization is named "Premature optimization" - well-known mistake of software developers. Try to avoid it.

Upvotes: 1

Ashok Raj
Ashok Raj

Reputation: 454

Obviously using

modifyValue(User.getSession().getSessionValue());


would be little faster at times.
It's more of a compile time speed up than run time.
It's ok for small programms,
but when considering maintainablity of code,
using a variable might help in the longer run.

Upvotes: 1

duffymo
duffymo

Reputation: 308753

I don't believe there's a speed difference. This is the kind of micro-optimization that's likely to lead you astray.

There might be a a thread safety issue with a writable private member variable if it's not synchronized properly.

I would prefer the first one because it avoids unnecessary writable state.

Upvotes: 9

Related Questions