Results 1 to 7 of 7
  1. #1
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Question Do getters & setters effect the performance?

    Hello,
    I need a high performance in my program in terms of running time. I was wondering if the getter and setter methods also effect the performance?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think it was Donald Knuth who said, "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil".

    And it was Brian Goetz who said, "write dumb code" in an article which you should read: Write Dumb Code

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    All methods (and constructors) affect performance. Basically "performance" means how well methods do what they are supposed to do.

  4. #4
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    All methods (and constructors) affect performance. Basically "performance" means how well methods do what they are supposed to do.
    So can we say "Calling a method also affects the performance"? Because you know, getters and setters, they just return values or assign values. They do well what they are expected to do in that sense.

  5. #5
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I think it was Donald Knuth who said, "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil".

    And it was Brian Goetz who said, "write dumb code" in an article which you should read: Write Dumb Code

    Thank you! Very good article. I will keep that in mind.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    getters and setters, they just return values or assign values

    The notion of getters and setters is a somewhat informal one. But even if you assume a specific implementation for them (I didn't) then it is still true that invoking them 10M times in a specified context will take so many ms. And that number of ms is their efficiency.

    Or are you asking whether calling a setter is slower than making an assignment to a public variable "inline"? I don't think it is likely to have a measureable effect: but the only way of telling for sure would be to try and measure it. Personally I wouldn't bother: the question falls well within Knuth's 97%.

    I'm not sure I would describe the time cost as "inefficiency" either. Race cars are fast. But typically their drivers need sponsorship to pay for the fuel and upkeep of them because they so horribly inefficient. (Software needs upkeep too and having variables with sane access modifiers set with setters is one way of keeping the maintainence cost down). They're fast, but they actually require some other vehicle to deliver them to the race track!

    Where you have a concept like efficiency that depends on all sorts of factors (memory use and time for small bits of the code to execute, through to useability and long term maintenance) then fixating on one of them will very likely lead to diminishing and eventually negative returns.

    Notice how the factors operate on different scales (of time and code). Knuth went further than saying that chasing small efficiencies is a waste of time. Premature optimisation - that is optimising code before that code even has a context within which it operates - can be counterproductive by fighting against the greater effeciencies to be had at broader scales. You need the code up and running as a whole before you can even start to identify what could usefully be sped up.

    Everyone has their favourite idioms that are better because they are more efficient. (Using the StringBuilder/Buffer springs to mind) I think the rarity of these proves Knuth's rule. I've haven't read the Goetz article for a while, but I seem to remember he makes a strong case for viewing efficiency within the context of the compiler and the overall architecture. And draws the consclusion that you should write straightforward code (a big win for setters/getters.)
    Last edited by pbrockway2; 03-12-2011 at 07:03 PM.

  7. #7
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    The notion of getters and setters is a somewhat informal one. But even if you assume a specific implementation for them (I didn't) then it is still true that invoking them 10M times in a specified context will take so many ms. And that number of ms is their efficiency.

    Or are you asking whether calling a setter is slower than making an assignment to a public variable "inline"? I don't think it is likely to have a measureable effect: but the only way of telling for sure would be to try and measure it. Personally I wouldn't bother: the question falls well within Knuth's 97%.

    I'm not sure I would describe the time cost as "inefficiency" either. Race cars are fast. But typically their drivers need sponsorship to pay for the fuel and upkeep of them because they so horribly inefficient. (Software needs upkeep too and having variables with sane access modifiers set with setters is one way of keeping the maintainence cost down). They're fast, but they actually require some other vehicle to deliver them to the race track!

    Where you have a concept like efficiency that depends on all sorts of factors (memory use and time for small bits of the code to execute, through to useability and long term maintenance) then fixating on one of them will very likely lead to diminishing and eventually negative returns.

    Notice how the factors operate on different scales (of time and code). Knuth went further than saying that chasing small efficiencies is a waste of time. Premature optimisation - that is optimising code before that code even has a context within which it operates - can be counterproductive by fighting against the greater effeciencies to be had at broader scales. You need the code up and running as a whole before you can even start to identify what could usefully be sped up.

    Everyone has their favourite idioms that are better because they are more efficient. (Using the StringBuilder/Buffer springs to mind) I think the rarity of these proves Knuth's rule. I've haven't read the Goetz article for a while, but I seem to remember he makes a strong case for viewing efficiency within the context of the compiler and the overall architecture. And draws the consclusion that you should write straightforward code (a big win for setters/getters.)

    My question was regarding using public variables rather than having them private and calling their setters and getters. If it is measured by ms even after invoking them 10m times, then it does not make sense to remove those. I think you made a good point that an efficient algorithm is efficient regardless of the code. I think it is a waste of time to chase this kind of minor savings in time.
    Thank you!

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2010, 11:01 AM
  2. Positions and values, getters, setters
    By Malus in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 05:55 PM
  3. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 01:47 AM
  4. Replies: 1
    Last Post: 02-19-2009, 03:50 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 10:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •