why prefix increment is faster than postfix increment?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jrefactors@hotmail.com

    why prefix increment is faster than postfix increment?

    I heard people saying prefix increment is faster than postfix
    incerement, but I don't know what's the difference. They both are
    i = i+1.

    i++
    ++i

    Please advise. thanks!!

  • Jonathan Mcdougall

    #2
    Re: why prefix increment is faster than postfix increment?

    jrefactors@hotm ail.com wrote:[color=blue]
    > I heard people saying prefix increment is faster than postfix
    > incerement, but I don't know what's the difference. They both are
    > i = i+1.
    >
    > i++
    > ++i[/color]



    And while you're there, read the whole faq.


    Jonathan

    Comment

    • John Carson

      #3
      Re: why prefix increment is faster than postfix increment?

      <jrefactors@hot mail.com> wrote in message
      news:1129968097 .386674.50960@g 44g2000cwa.goog legroups.com[color=blue]
      > I heard people saying prefix increment is faster than postfix
      > incerement, but I don't know what's the difference. They both are
      > i = i+1.[/color]

      They are obviously not both the same. The prefix form returns the value of
      the variable after the incrementation and the postfix form returns the value
      of the variable before the incrementation.

      For the postfix form, the simple way to implement it is for the compiler to
      produce code that creates a temporary that stores the original value and
      then to return this value. For a built-in type, this is a cheap operation.
      For a class object, it may be an expensive operation.

      --
      John Carson

      Comment

      • Christian Bau

        #4
        Re: why prefix increment is faster than postfix increment?

        In article <1129968097.386 674.50960@g44g2 000cwa.googlegr oups.com>,
        jrefactors@hotm ail.com wrote:
        [color=blue]
        > I heard people saying prefix increment is faster than postfix
        > incerement, but I don't know what's the difference. They both are
        > i = i+1.
        >
        > i++
        > ++i
        >
        > Please advise. thanks!![/color]

        The C answer and the C++ answer for the built-in operators are: Whoever
        makes that claim is not only clueless, but is also the type of dangerous
        clueless person who thinks they have a clue. Don't take _any_ advice of
        them. Ever.

        I don't think you are interested in the C++ answer for operator
        overloading yet.

        Comment

        • Dave Rahardja

          #5
          Re: why prefix increment is faster than postfix increment?

          On 22 Oct 2005 01:01:37 -0700, jrefactors@hotm ail.com wrote:
          [color=blue]
          >I heard people saying prefix increment is faster than postfix
          >incerement, but I don't know what's the difference. They both are
          >i = i+1.
          >
          >i++
          >++i
          >
          >Please advise. thanks!![/color]

          When i is a C++ object with the appropriate operators defined, the prefix
          version may eliminate the creation of a temporary object.

          -dr

          Comment

          • Kaz Kylheku

            #6
            Re: why prefix increment is faster than postfix increment?

            jrefactors@hotm ail.com wrote:[color=blue]
            > I heard people saying prefix increment is faster than postfix
            > incerement, but I don't know what's the difference. They both are
            > i = i+1.
            >
            > i++
            > ++i[/color]

            If you are throwing away the result, there is no semantic difference at
            all, because the only difference between ++i and i++ is whether i or i
            + 1 is returned.

            If you don't throw away the result, then they are different operators;
            you can't substitute one without the other without making compensating
            changes in the surrounding program! You then end up with two different
            programs that you have to compare as such. If there is a performance
            difference between those programs, it's a result of not just changing
            the i++ to ++i, or vice versa, but also a consequence of those other
            compensating changes, and how your particular compiler and machine
            deals with everything as a whole.

            Note that in C++ (this is cross-posted to comp.lang.c++), both forms of
            the operator can be user-defined. If you are dealing with a choice
            between two forms of the user-defined operator, and performance is
            critical, you obviously have to take that into consideration!

            Comment

            • Gordon Burditt

              #7
              Re: why prefix increment is faster than postfix increment?

              >I heard people saying prefix increment is faster than postfix[color=blue]
              >incerement, but I don't know what's the difference. They both are[/color]

              Any claim that X is faster than Y that doesn't specify a specific
              compiler and platform is FALSE. Even if Y is "do X 1000000 times".

              Gordon L. Burditt

              Comment

              • Dave Rahardja

                #8
                Re: why prefix increment is faster than postfix increment?

                On Sat, 22 Oct 2005 17:50:01 -0000, gordonb.foi6x@b urditt.org (Gordon Burditt)
                wrote:
                [color=blue][color=green]
                >>I heard people saying prefix increment is faster than postfix
                >>incerement, but I don't know what's the difference. They both are[/color]
                >
                >Any claim that X is faster than Y that doesn't specify a specific
                >compiler and platform is FALSE. Even if Y is "do X 1000000 times".[/color]

                I think the original poster heard one of those "rules of thumb" that aren't
                absolutely true or mathematically proven, but is considered a good
                approximation of the truth most of the time. I also assume that the OP wanted
                to know what the motivation was behind the saying.

                -dr

                Comment

                • Greg

                  #9
                  Re: why prefix increment is faster than postfix increment?

                  jrefactors@hotm ail.com wrote:[color=blue]
                  > I heard people saying prefix increment is faster than postfix
                  > incerement, but I don't know what's the difference. They both are
                  > i = i+1.
                  >
                  > i++
                  > ++i
                  >
                  > Please advise. thanks!![/color]

                  Consider this program:

                  void PrintElement(co nst std::vector<int >::iterator& i)
                  {
                  std::cout << *i << " ";
                  }

                  int main()
                  {
                  std::vector<int > v;

                  v.push_back(1); v.push_back(2); v.push_back(3);

                  std::vector<int >::iterator i = v.begin();

                  while (i != v.end())
                  PrintElement( i++ );

                  while (i != v.begin())
                  PrintElement( --i );
                  }

                  Which PrintElement() call is like the more efficient one: the one with
                  the postincremented parameter (i++) or the pre-decremented (--i)
                  parameter? Or is there no reason to think that there would be a
                  difference?

                  In this case, it is likely that first PrintElement call with the
                  postfix incremented paramter has more overhead than the second, because
                  the compiler must increment the iterator i before it calls
                  PrintElement. But when the call to PrintElement is made, the compiler
                  must pass the value of i (or a reference to a temporary copy of i) that
                  i had before it was incremented. Therefore the compiler has little
                  choice but to make a copy of i before incrementing i, so that it has an
                  iterator with which it can call PrintElement.

                  The second PrintElement call applies a prefix operator to the paramter;
                  the compiler can therefore pass i directly to PrintElement, since its
                  incremented value is the appropriate value to pass to PrintElement.

                  Of course, the difference is not likely to be great, but there is
                  nonetheless a basis for expecting postfix operators to be less
                  efficient than prefix operators, especially when applied to parameters
                  in a function call.

                  Greg

                  Comment

                  • Martin Ambuhl

                    #10
                    Re: why prefix increment is faster than postfix increment?

                    Greg wrote:
                    [color=blue]
                    > Consider this program:
                    >
                    > void PrintElement(co nst std::vector<int >::iterator& i)
                    > {
                    > std::cout << *i << " ";
                    > }[/color]
                    [etc.]

                    When you respond to posts which are crossposted to <news:comp.lang .c>
                    and <news:comp.lang .c++>, try to give answers that are acceptable in
                    both. You have posted a bunch of compilation errors to
                    <news:comp.lang .c>. There is no need to consider your code at all.

                    Comment

                    • Greg

                      #11
                      Re: why prefix increment is faster than postfix increment?


                      Gordon Burditt wrote:[color=blue][color=green]
                      > >I heard people saying prefix increment is faster than postfix
                      > >incerement, but I don't know what's the difference. They both are[/color]
                      >
                      > Any claim that X is faster than Y that doesn't specify a specific
                      > compiler and platform is FALSE. Even if Y is "do X 1000000 times".[/color]

                      Not necessarily. If one can show that Y performs every operation that X
                      performs, and then has to perform additional operations outside of that
                      set and that require a measurable amount of time to complete, then one
                      would have successfully proven that X is faster than Y. And in fact
                      that is the case here: the postincrement operator may have to perform
                      an additional copy operation that the prefix version does not have to
                      perform. Otherwise the amount of work required of each is the same.

                      Greg

                      Comment

                      • Michael Mair

                        #12
                        Re: why prefix increment is faster than postfix increment?

                        Greg wrote:[color=blue]
                        > Gordon Burditt wrote:
                        >[color=green][color=darkred]
                        >>>I heard people saying prefix increment is faster than postfix
                        >>>incerement , but I don't know what's the difference. They both are[/color]
                        >>
                        >>Any claim that X is faster than Y that doesn't specify a specific
                        >>compiler and platform is FALSE. Even if Y is "do X 1000000 times".[/color]
                        >
                        > Not necessarily. If one can show that Y performs every operation that X
                        > performs, and then has to perform additional operations outside of that
                        > set and that require a measurable amount of time to complete, then one
                        > would have successfully proven that X is faster than Y. And in fact
                        > that is the case here: the postincrement operator may have to perform
                        > an additional copy operation that the prefix version does not have to
                        > perform. Otherwise the amount of work required of each is the same.[/color]

                        Nope. Maybe in source code but not necessarily in the generated code.
                        The optimisation may have a stage which looks for certain patterns;
                        it is very well possible that the seemingly slower code qualifies for
                        the optimisation but the "faster" code does not. This may be the
                        compiler's fault or yours, depending on the problem at hand.

                        So, you are right most of the time but not always. However, C++ coding
                        guidelines often bring the ++()/()++ including the caveats as an
                        example of "avoiding premature pessimization" and rightly so.


                        Cheers
                        Michael
                        --
                        E-Mail: Mine is an /at/ gmx /dot/ de address.

                        Comment

                        • Gordon Burditt

                          #13
                          Re: why prefix increment is faster than postfix increment?

                          >> >I heard people saying prefix increment is faster than postfix[color=blue][color=green][color=darkred]
                          >> >incerement, but I don't know what's the difference. They both are[/color]
                          >>
                          >> Any claim that X is faster than Y that doesn't specify a specific
                          >> compiler and platform is FALSE. Even if Y is "do X 1000000 times".[/color]
                          >
                          >Not necessarily. If one can show that Y performs every operation that X
                          >performs, and then has to perform additional operations outside of that
                          >set and that require a measurable amount of time to complete, then one
                          >would have successfully proven that X is faster than Y.[/color]

                          Not unless you can prove that the compiler generates the code that
                          way, and in order to do that, you have to choose a specific compiler.
                          You cannot test *ALL* compilers, including ones written between
                          your test and publishing the results. And you can't prove, for
                          example, that the compiler doesn't add a time-wasting loop to X but
                          not to Y, without referring to a specific compiler.

                          There are some situations where doing more can be faster.
                          For example repeating this statement 1000000 times could be
                          faster than doing it 10 times:
                          unsigned int x;

                          x = x << 1;

                          since if the width of x is less than 1000000 bits, this results in
                          a constant independent of the original value of x, and the compiler
                          might realize this, but if the width of x is greater than 10, and it
                          must be, it has to shift x.
                          [color=blue]
                          >And in fact
                          >that is the case here: the postincrement operator may have to perform
                          >an additional copy operation that the prefix version does not have to
                          >perform. Otherwise the amount of work required of each is the same.[/color]

                          You're assuming things about the underlying instruction set that
                          may not be true, and assuming that the compiler doesn't do a
                          poor job of generating code for one and a good job for the other.
                          The effect of a cache hit/miss can also do funny things to code
                          that looks like it should run in the same time as other code.

                          Gordon L. Burditt

                          Comment

                          • Christian Bau

                            #14
                            Re: why prefix increment is faster than postfix increment?

                            In article <1130055121.143 006.220940@f14g 2000cwb.googleg roups.com>,
                            "Greg" <greghe@pacbell .net> wrote:
                            [color=blue]
                            > Gordon Burditt wrote:[color=green][color=darkred]
                            > > >I heard people saying prefix increment is faster than postfix
                            > > >incerement, but I don't know what's the difference. They both are[/color]
                            > >
                            > > Any claim that X is faster than Y that doesn't specify a specific
                            > > compiler and platform is FALSE. Even if Y is "do X 1000000 times".[/color]
                            >
                            > Not necessarily. If one can show that Y performs every operation that X
                            > performs, and then has to perform additional operations outside of that
                            > set and that require a measurable amount of time to complete, then one
                            > would have successfully proven that X is faster than Y.[/color]

                            One would have proven no such thing.

                            Consider this:

                            double x, y;
                            memcpy (&x, &y, sizeof (x) - 1);
                            memcpy (&x, &y, sizeof (x));

                            A good compiler will translate the second memcpy to a simple assignment
                            of a double variable, for example:

                            Load double y into register
                            Store register into double x.

                            In fact, if this is the only time the address of x and y is taken, it is
                            quite possible that x and y are still kept in floating-point registers,
                            in which case this is a very fast register-to-register assignment.

                            On the other hand, the first memcpy will have a much more difficult
                            implementation, even though it copies one byte less. Not only will it
                            produce much more code, on most current processors that code will
                            execute considerably slower.

                            Comment

                            • Jack Klein

                              #15
                              Re: why prefix increment is faster than postfix increment?

                              On 22 Oct 2005 01:01:37 -0700, jrefactors@hotm ail.com wrote in
                              comp.lang.c:
                              [color=blue]
                              > I heard people saying prefix increment is faster than postfix
                              > incerement, but I don't know what's the difference. They both are
                              > i = i+1.[/color]

                              What people are these? What are their credentials so that you, or we,
                              should place any confidence in their opinions?
                              [color=blue]
                              > i++
                              > ++i
                              >
                              > Please advise. thanks!![/color]

                              One possible item of advice would be for you to associate with
                              different people.

                              --
                              Jack Klein
                              Home: http://JK-Technology.Com
                              FAQs for
                              comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                              comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                              alt.comp.lang.l earn.c-c++

                              Comment

                              Working...