Do accessors lower performance?

Discussion in 'Java' started by Zhao, Oct 10, 2003.

  1. Zhao

    Zhao Guest

    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
     
    Zhao, Oct 10, 2003
    #1
    1. Advertising

  2. 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
     
    Marco Schmidt, Oct 10, 2003
    #2
    1. Advertising

  3. 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.
     
    Michael Borgwardt, Oct 10, 2003
    #3
  4. 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?

    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
     
    Robert Olofsson, Oct 11, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flip
    Replies:
    2
    Views:
    323
  2. Jarma
    Replies:
    0
    Views:
    307
    Jarma
    Aug 29, 2003
  3. Lucas Hofman
    Replies:
    13
    Views:
    736
  4. Andreas Kostyrka
    Replies:
    0
    Views:
    428
    Andreas Kostyrka
    Dec 17, 2004
  5. Raga
    Replies:
    7
    Views:
    6,413
    ajrobb
    Apr 5, 2007
Loading...

Share This Page