3

Pretty simple question, I have an object (say Obj) which has member variable (say "var"). In one function I need to access this property multiple times (4 to 5 times). What I am doing right now is everytime I do Obj.var to get the value. Is is right (from optimization point of view) to do that or I should store Obj.var in a temporary variable and then use this variable? I am looking for explanation from "how both approaches impact performance" point of view.

=====

What if Obj.var is replaced by Obj.getVar(), a getter method for that variable? How will it impact the performance?

  • 1
    Neither is "right." Either one is fine. Both are valid refactorings: [Explaining Variable](http://sourcemaking.com/refactoring/introduce-explaining-variable); [Replace with Query](http://sourcemaking.com/refactoring/replace-temp-with-query) – markspace Nov 19 '14 at 03:44
  • are you getting the property 4-5 times in a function? – billz Nov 19 '14 at 03:44
  • possible duplicate: [Calling getters on an object vs. storing it as a local variable (memory footprint, performance)](http://stackoverflow.com/questions/19445737/calling-getters-on-an-object-vs-storing-it-as-a-local-variable-memory-footprin) – victorantunes Nov 19 '14 at 03:58
  • @blitz: Yes, I am using referring it for more than 4 times. – Rajvidya Chandele Nov 19 '14 at 13:21

2 Answers2

3

Unless the variable is volatile, the JITC is free to do whatever it wants as long as it can prove that you're not overwriting the field in the meantime (without volatile, it doesn't care about what other threads do).

If you use no local variable, it will do it for you. It could also do it the other way, but I can hardly imagine any case when it makes sense (it could, when you run out of registers).

So it doesn't matter for performance(*), but for readability, the local is nearly always better, so go for it.


(*) On Android, the situation may differ as it's not as smart as server class JVM.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
1

It depends on the type of the field ("Obj.var"):

  • If it's of the primitive type (int, float, char etc.) you probably won't see any impact of performance (since primitive types are tiny: 8 bytes max for long and double).

  • If it's an Object itself (including arrays of primitives) assigning it to the local variable won't improve anything, because what assigned is the reference to the Object, so by accessing local copy you essentially will be accessing the original Object (Obj.var). Speaking of the local copy, there is a way to create "true" local copy (i.e. the one that is independent from the original Object Obj.var, not just another reference to the same object) by cloning this variable (if type of Obj.var supports it). But by doing that you'd probably loose rather than gain a bit of performance, if anything: in addition to all the expenses for access to the object (which you pay in either case), you will also pay for the cloning (copying) of object into the local scope.

striving_coder
  • 798
  • 1
  • 5
  • 7