• Post Reply Bookmark Topic Watch Topic
  • New Topic

field call vs. getField  RSS feed

 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is kind of a dinky/newbiesque question, but is a call to getField() much/any slower than directly referencing object.field?
for example...

is a direct call to animal.numFeet any faster than animal.getNumFeet()?
I'm just looking at making lots of calls and I didn't know if the tradeoff of directly accessing numFeet(for speed) would be worth losing the modularity/security of making numFeet private and forcing access through getNumFeet()
Thanks...
[ March 05, 2002: Message edited by: Chris Shepherd ]
[ March 05, 2002: Message edited by: Chris Shepherd ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be some difference in performance simply because it takes more bytecode for the compiler to generate a return as opposed to a direct access of the variable (some compilers may optimize the code to actually directly return the value of the variable, but I'm not sure.)
You really don't want to violate encapsulation though for the sake of performance unless there are just no other alternatives which there usually are.
For instance, if you create separate threads or a thread pool in the scheme of your application you can increase performance enormously to an extent.
So try to avoid direct access outside the class unless there is just no alternative.
Hope this helps
 
Thomas Smets
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java optimizer use what they call MASSIVE OPTIMIZAZTION. Buzz word to say we convert all the accessors inthe source (in the java-code), into direct access in the byte-code.
Enough said in that matter I believe what you should consider is why you are using accessor instead of direct access ?
I that respect, using the accessor but making the variables public should start improving performance (as the VM does not need to check access modifier for a particular method / attirbute).
Still doing that you should enforce people to use accessors but have all the variables private ! Please note that this is really dangerous at Project level, but ...
Also if you are dealing with performance issues I would suggest that you look at another VM for you deployment environment. IBM VM's have a strong reputation which I have never seen been critized or people aknowledging enormous doubt about their superiority (but everything can change )!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas SMETS:
I that respect, using the accessor but making the variables public should start improving performance (as the VM does not need to check access modifier for a particular method / attirbute).

I have never before heard something like this. Could you please elaborate? Are there any articles on this subject?
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas SMETS:
I that respect, using the accessor but making the variables public should start improving performance (as the VM does not need to check access modifier for a particular method / attirbute).
Still doing that you should enforce people to use accessors but have all the variables private ! Please note that this is really dangerous at Project level, but ...

You are correct that it will increase speed because the interpreter does not need to check. You are also correct that it is extremely dangerous! In my models of software costs, it's not worth it, because developers will break the rules.
You can achieve the same results by using many obfuscators like OptimizeIt. It will leave your source code untouched, but modify the generated bytecode. You get the best of both worlds. (You can probably fairly easily right your own tool to do this, too.)
--Mark
PS I suspect that most JITs would make this a moot point.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Herschberg:
PS I suspect that most JITs would make this a moot point.

Yes, that is my guess, too. In fact, a modern hotspot engine might even inline the getter, so that there would be actually *no* difference in performance.
Did anybody *try* profiling the difference?
Curious, Ilja
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!