Like Tree3Likes
  • 1 Post By Justin H
  • 1 Post By C_ntua
  • 1 Post By grumpy

Which method is more efficient?

This is a discussion on Which method is more efficient? within the C++ Programming forums, part of the General Programming Boards category; I'm keeping a account of the # of buttons created in my program. And from several places that # is ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    30

    Which method is more efficient?

    I'm keeping a account of the # of buttons created in my program. And from several places that # is used to pull data from a vector, etc.

    Is it more efficient to to increment an integer with the # of buttons available/created, or to always call the vector size method?
    baxy likes this.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Always use size method. The vector already increments internally an integer for you and the size method returns it.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,313
    There is no rule that says a vector keeps its size in a data member that is returned by size().

    To answer the original question, do whatever produces the correct results for your program in the simplest manner possible. Worrying about efficiency at this stage is called "premature optimisation" for a reason. So don't worry about it.

    Unless your program has exhibited a particular performance problem and you have used a profiler to identify a particular section of code as the cause, you have no reason to prefer one method over the other. Odds are, whichever option you pick to "optimise" performance, unless you have used a profiler, will be wrong.
    Elysia likes this.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,426
    Quote Originally Posted by grumpy View Post
    Odds are, whichever option you pick to "optimise" performance, unless you have used a profiler, will be wrong.
    Statistically speaking, the OP has an equal chance of being right or wrong, since it's a simple "is it more efficient?" sort of question. However, to address the OP's question, I use .size() in a lot of code, and I haven't seen any slowdown because of it.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,313
    Quote Originally Posted by Elkvis View Post
    Statistically speaking, the OP has an equal chance of being right or wrong, since it's a simple "is it more efficient?" sort of question.
    You're assuming all that programmers do is flip a coin. In practice, programmers who are trying to optimise use invalid criteria for deciding what to optimise, so tend to skew the odds against themselves.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,426
    Quote Originally Posted by grumpy View Post
    You're assuming all that programmers do is flip a coin. In practice, programmers who are trying to optimise use invalid criteria for deciding what to optimise, so tend to skew the odds against themselves.
    but a programmer such as the OP, who doesn't have a preconceived notion of the right way, is equally likely to choose the right or the wrong option. you'll note that I didn't say "all programmers." I said "the OP."

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Really if .size() was not efficient enough and would cause you to need to maintain the size separately, then it would defeat the purpoe of having a .size() member.
    The same argument doesn't quite work for std::list though, unfortunately. They left that one open to being O(n) due to other advantages that has.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,313
    Quote Originally Posted by Elkvis View Post
    but a programmer such as the OP, who doesn't have a preconceived notion of the right way, is equally likely to choose the right or the wrong option. you'll note that I didn't say "all programmers." I said "the OP."
    And I disagree.

    The OP has asked which technique is more efficient. That is a particular mindset. There is a whole bunch of issues around trying to maximise efficiency, such as it being hard to measure, variation (for a given code construct) between compiler and library implementations, and the "obvious" rules of thumb often being wrong (or, at best, implementation specific) in practice.

    By asking the wrong question, and assuming there is only one answer to it, the OP has stacked the odds against himself. Particularly if he keeps asking the same questions for all sorts of code constructs.

    Yes, there are advantages of some techniques over others. But the differences in efficiency are trade-offs, not absolutes. And the effort of premature optimisation exceeds the benefits in most cases.
    Last edited by grumpy; 12-07-2012 at 02:07 PM.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,426
    grumpy, you make a few very valid points. as a beginner, it's probably just best to use the tools that are available at first, without trying to understand why they work the way they do, and without worrying too much about performance. that can come later, once the basic understanding of the language and the technology is established.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between this->method() and method()
    By nacho4d in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2009, 03:11 PM
  2. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 3
    Last Post: 09-19-2009, 10:35 AM
  3. most efficient method?
    By reRanger in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2004, 11:39 PM
  4. which method is more efficient?
    By lambs4 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-27-2001, 11:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21