Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Do accessors lower performance?

Reply
Thread Tools

Do accessors lower performance?

 
 
Zhao
Guest
Posts: n/a
 
      10-10-2003
I am wondering how the latest Java compiler/JVM deal with accessor
(getXXX setXXX). Is there any overhead using them comparing to making
class members public and access them directly?

thanks
 
Reply With Quote
 
 
 
 
Marco Schmidt
Guest
Posts: n/a
 
      10-10-2003
Zhao:

>I am wondering how the latest Java compiler/JVM deal with accessor
>(getXXX setXXX). Is there any overhead using them comparing to making
>class members public and access them directly?


Depends. If your getXXX is called often, it may get inlined by the JIT
compiler. Then there's no overhead. It probably helps if you declare
the getXXX method as final.

Anyway, I would do measurements before sacrificing good data
abstraction practices like encapsulating data fields with getters and
setters.

If you work a lot on a specific data structure, maybe it's a good idea
to copy its content, let the algorithm work directly on the copy and
write it back after that.

Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-10-2003
Zhao wrote:
> I am wondering how the latest Java compiler/JVM deal with accessor
> (getXXX setXXX). Is there any overhead using them comparing to making
> class members public and access them directly?


Practically none. Don't worry about it.

 
Reply With Quote
 
Robert Olofsson
Guest
Posts: n/a
 
      10-11-2003
Zhao ((E-Mail Removed)) wrote:
: I am wondering how the latest Java compiler/JVM deal with accessor
: (getXXX setXXX). Is there any overhead using them comparing to making
: class members public and access them directly?

Hotspot inlines quite much nowdays. If you really care about
performance I suggest you get a profiler and try to measure the cost
of the accessors, most of the time there wont be any such cost.

However if you profile your application from start to finish with
method tracing enabled hotspot will not inline code. You have to start
your application without method tracing and enable it after some time
to see what got inlined. This is kind of evil, since this means that
many will profile and find a trace that does not look like the one on
the production server....

/robo
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessors vs public data members Zap C++ 10 12-23-2004 09:02 PM
The real issue with accessors and mutators Steven T. Hatton C++ 12 11-10-2004 12:41 AM
accessors Mark A. Gibbs C++ 12 08-19-2004 04:16 PM
question about accessors and constants and variable declaration locations Flip Java 2 05-25-2004 02:28 PM
Accessors by value or reference? Jarma C++ 0 08-29-2003 08:26 PM



Advertisments