style and performance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    #1

    style and performance

    Hi

    while I am reading this C++ book, I noticed the iterator is being
    used to loop through a container, say a vector. I am used to use
    "for(int=0;i<ve c.size();++i)" which is better to use?

    also I noticed the author references a class variable by using
    class-identifier.vari able-identifier instead of get_variable(); again
    which is better?

    I am just after improving the performance of my code.
    thanks
  • Alf P. Steinbach

    #2
    Re: style and performance

    * Gary Wessle:
    >
    while I am reading this C++ book, I noticed the iterator is being
    used to loop through a container, say a vector. I am used to use
    "for(int=0;i<ve c.size();++i)" which is better to use?
    >
    also I noticed the author references a class variable by using
    class-identifier.vari able-identifier instead of get_variable(); again
    which is better?
    >
    I am just after improving the performance of my code.
    Regarded as style issues your questions are meaningful, but the answers
    depend on the context. E.g., using indexing of a vector can be more
    clear but can restrict the code to indexable data representations .
    Likewise, direct access of object data members can be more clear and
    reduce the code (nearly always good), especially for simple cases such
    as a 2D point, but can also restrict -- e.g. representation, naming,
    the possibility of working on a pure interface, and more.

    Regarded as questions about performance the questions are not very
    meaningful.

    For performance: use efficient algorithms, use common sense, then if
    that isn't enough measure, measure, measure.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Phlip

      #3
      Re: style and performance

      Gary Wessle wrote:
      while I am reading this C++ book, I noticed the iterator is being
      used to loop through a container, say a vector. I am used to use
      "for(int=0;i<ve c.size();++i)" which is better to use?
      Use an iterator.

      Old-fashioned code used an index because the iterator concept was not
      invented yet. An iterator more closely matches the code's concept.
      also I noticed the author references a class variable by using
      class-identifier.vari able-identifier instead of get_variable(); again
      which is better?
      Generally, always make primitive things private. That includes a classes
      data members. Ideally, you should tell a class object What to do, but not
      How to do it. The getVariable() idiom is sometimes required, but it
      indicates you are asking an object for its data so you can do its actions
      for it. You should instead tell the object what to do, and not consider how
      it uses its variables to do it.
      I am just after improving the performance of my code.
      Look up "premature optimization is the root of all evil".

      You are asking how to prematurely optimize. The most important resource to
      optimize is programming time. You should write the clearest most obvious
      code you can. It should use iterators and member functions, not indices or
      raw data. Sometimes one is faster, sometimes the other is faster, but you
      never know.

      To optimize, finish your program, then see if it's slow. With modern CPUs,
      you will need to program for a very long time before your code gets slow.
      Only at that time should you optimize it. Otherwise you will most likely
      waste time optimizing the wrong thing. And such optimizations invariably
      make code harder to read. So first work to make the code easy to read and
      change.

      It's easier to make beautiful code fast than to make fast code beautiful.
      And only beautiful code can keep development fast.

      --
      Phlip
      http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


      Comment

      • Alan Johnson

        #4
        Re: style and performance

        Gary Wessle wrote:
        Hi
        >
        while I am reading this C++ book, I noticed the iterator is being
        used to loop through a container, say a vector. I am used to use
        "for(int=0;i<ve c.size();++i)" which is better to use?
        >
        also I noticed the author references a class variable by using
        class-identifier.vari able-identifier instead of get_variable(); again
        which is better?
        >
        I am just after improving the performance of my code.
        thanks
        Another option is to avoid looping over containers. Instead use an
        algorithm such as std::for_each to apply some operation to a range of
        iterators.

        --
        Alan Johnson

        Comment

        • Daniel T.

          #5
          Re: style and performance

          In article <87y7u49h2t.fsf @localhost.loca ldomain>,
          Gary Wessle <phddas@yahoo.c omwrote:
          while I am reading this C++ book, I noticed the iterator is being
          used to loop through a container, say a vector. I am used to use
          "for(int=0;i<ve c.size();++i)" which is better to use?
          Performance wise, I doubt it makes a difference. Style wise, using
          iterators is more versatile because they can be used for lists, and I
          expect using iterators for deques would have better performance.
          also I noticed the author references a class variable by using
          class-identifier.vari able-identifier instead of get_variable(); again
          which is better?
          Again, performance wise probably none. Style wise... IMHO,
          'get_variable() ' is marginally better. The reason I think it is better
          is because it can easily be replaced by code that calculates the value
          on the fly, or loads the value from disk, or requests the value from
          some other object, or any of a number of different things, whereas using
          a public member-variable limits you to holding the value in ram in that
          object.

          Comment

          • Daniel T.

            #6
            Re: style and performance

            In article <BCRAg.1073$o27 .861@newssvr21. news.prodigy.co m>,
            "Phlip" <phlipcpp@yahoo .comwrote:
            Ideally, you should tell a class object What to do, but not How to do
            it.
            I can't agree with the above. Ideally, you should tell a class object
            what its environment is like, you shouldn't be telling it what to do.
            It's the object's job to decide what to do based on the information it
            is given.

            Let's start a war Phlip. :-)

            Comment

            • Phlip

              #7
              Re: style and performance

              Daniel T. wrote:
              Let's start a war Phlip. :-)
              Totally. Violent agreement is all the rage, these days, apparently...

              --
              Phlip
              http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


              Comment

              • Bo Persson

                #8
                Re: style and performance


                "Daniel T." <daniel_t@earth link.netskrev i meddelandet
                news:daniel_t-091B62.00093105 082006@news.wes t.earthlink.net ...
                In article <87y7u49h2t.fsf @localhost.loca ldomain>,
                Gary Wessle <phddas@yahoo.c omwrote:
                >
                >
                >also I noticed the author references a class variable by using
                >class-identifier.vari able-identifier instead of get_variable();
                >again
                >which is better?
                >
                Again, performance wise probably none. Style wise... IMHO,
                'get_variable() ' is marginally better. The reason I think it is
                better
                is because it can easily be replaced by code that calculates the
                value
                on the fly, or loads the value from disk, or requests the value from
                some other object, or any of a number of different things, whereas
                using
                a public member-variable limits you to holding the value in ram in
                that
                object.
                In which case get_variable() is a lie, if it isn't getting a variable.
                :-)

                Naming is important, and get_* and set_* almost always indicates a
                problem with the abstraction. For example, a set_position() should
                most likely be called something like move(). Having both set and get
                promotes horrible code like

                obj.set_x(obj.g et_x() + 12);

                :-(


                Bo Persson


                Comment

                • Daniel T.

                  #9
                  Re: style and performance

                  In article <4jj985F89ad4U1 @individual.net >, "Bo Persson" <bop@gmb.dk>
                  wrote:
                  "Daniel T." <daniel_t@earth link.netskrev i meddelandet
                  news:daniel_t-091B62.00093105 082006@news.wes t.earthlink.net ...
                  In article <87y7u49h2t.fsf @localhost.loca ldomain>,
                  Gary Wessle <phddas@yahoo.c omwrote:

                  also I noticed the author references a class variable by using
                  class-identifier.vari able-identifier instead of get_variable();
                  again
                  which is better?
                  Again, performance wise probably none. Style wise... IMHO,
                  'get_variable() ' is marginally better. The reason I think it is
                  better
                  is because it can easily be replaced by code that calculates the
                  value
                  on the fly, or loads the value from disk, or requests the value from
                  some other object, or any of a number of different things, whereas
                  using
                  a public member-variable limits you to holding the value in ram in
                  that
                  object.
                  >
                  In which case get_variable() is a lie, if it isn't getting a variable.
                  :-)
                  'get_informatio n()' would be better.
                  Naming is important, and get_* and set_* almost always indicates a
                  problem with the abstraction. For example, a set_position() should
                  most likely be called something like move(). Having both set and get
                  promotes horrible code like
                  >
                  obj.set_x(obj.g et_x() + 12);
                  All (non-static) member-functions either return state information about
                  the object or change state information. In that sense, they are all
                  either getters or setters, and saying that they are "a problem with
                  abstraction" becomes a huge problem...

                  Comment

                  Working...