++u or u++ which is faster?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The Doctor

    ++u or u++ which is faster?

    Hi all,

    Recently I was asked the question about whether ++U or U++ is
    faster if U is a user defined type. What's the answer?

    Thank you
  • Sumit Rajan

    #2
    Re: ++u or u++ which is faster?

    The Doctor wrote:[color=blue]
    > Hi all,
    >
    > Recently I was asked the question about whether ++U or U++ is
    > faster if U is a user defined type. What's the answer?
    >
    > Thank you[/color]




    --
    Sumit Rajan <sumitrajan AT alexandria DOT cc>

    Comment

    • JKop

      #3
      Re: ++u or u++ which is faster?

      The Doctor posted:
      [color=blue]
      > Hi all,
      >
      > Recently I was asked the question about whether ++U or U++ is
      > faster if U is a user defined type. What's the answer?
      >
      > Thank you[/color]


      ++u is faster.


      Always use ++u. Never use u++ unless you need to.


      -JKop

      Comment

      • Vaclav Haisman

        #4
        Re: ++u or u++ which is faster?

        Usually operator ++ (int) is implemented like this:

        T operator ++ (int)
        {
        T tmp = *this;
        ++*this;
        return tmp;
        }

        If compiler isn't able to optimize away the initialization of the temporary
        then using u++ is slower. That's why it is usually better to use
        pre-increment/pre-decrement instead of post-decrement/post-increment.

        VH

        "The Doctor" <doctor@nospam. com> wrote in message
        news:Xns956FD71 947B8AdocwhoATb igpondDOTne@61. 9.191.5...[color=blue]
        > Hi all,
        >
        > Recently I was asked the question about whether ++U or U++ is
        > faster if U is a user defined type. What's the answer?
        >
        > Thank you[/color]


        Comment

        • Method Man

          #5
          Re: ++u or u++ which is faster?


          "The Doctor" <doctor@nospam. com> wrote in message
          news:Xns956FD71 947B8AdocwhoATb igpondDOTne@61. 9.191.5...[color=blue]
          > Hi all,
          >
          > Recently I was asked the question about whether ++U or U++ is
          > faster if U is a user defined type. What's the answer?
          >
          > Thank you[/color]

          The simple answer is when using U++, the object U needs to get copied twice
          whereas with ++U, it need only be copied once.

          So use ++U unless you specifically require the value of the post-increment.


          Comment

          • Chris Theis

            #6
            Re: ++u or u++ which is faster?


            "JKop" <NULL@NULL.NULL > schrieb im Newsbeitrag
            news:I0d5d.3198 6$Z14.10503@new s.indigo.ie...[color=blue]
            > The Doctor posted:
            >[color=green]
            > > Hi all,
            > >
            > > Recently I was asked the question about whether ++U or U++ is
            > > faster if U is a user defined type. What's the answer?
            > >
            > > Thank you[/color]
            >
            >
            > ++u is faster.[/color]

            This general statement is not true. It depends on the datatype and the
            optimization of the compiler. Both types might be the same speed, but as you
            say it's generally wise to use ++u.
            [color=blue]
            >
            >
            > Always use ++u. Never use u++ unless you need to.
            >
            >
            > -JKop
            >[/color]


            Comment

            • Ioannis Vranos

              #7
              Re: ++u or u++ which is faster?

              JKop wrote:
              [color=blue]
              > ++u is faster.[/color]


              For built-in types their speed is the same. For user defined types it
              applies what you say.



              --
              Ioannis Vranos


              Comment

              • Simon Stienen

                #8
                Re: ++u or u++ which is faster?

                Ioannis Vranos <ivr@guesswh.at .grad.com> wrote:[color=blue]
                > JKop wrote:
                >[color=green]
                >> ++u is faster.[/color]
                >
                >
                > For built-in types their speed is the same. For user defined types it
                > applies what you say.[/color]

                So lets state:
                ++u is never slower.

                Now it's perfect ;P
                --
                Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
                »What you do in this world is a matter of no consequence,
                The question is, what can you make people believe that you have done.«
                -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

                Comment

                • Rolf Magnus

                  #9
                  Re: ++u or u++ which is faster?

                  Method Man wrote:
                  [color=blue]
                  >
                  > "The Doctor" <doctor@nospam. com> wrote in message
                  > news:Xns956FD71 947B8AdocwhoATb igpondDOTne@61. 9.191.5...[color=green]
                  >> Hi all,
                  >>
                  >> Recently I was asked the question about whether ++U or U++ is
                  >> faster if U is a user defined type. What's the answer?
                  >>
                  >> Thank you[/color]
                  >
                  > The simple answer is when using U++, the object U needs to get copied
                  > twice whereas with ++U, it need only be copied once.[/color]

                  What would that extra copy be needed for? I can see U++ doing at most 2
                  copies (but usually one - or if the function gets inlined and the return
                  value is not used even both - of them getting optimized away) and ++U doing
                  none at all. Consider the operators for an iterator into a linked list:

                  class mylistiterator
                  {
                  public:
                  //...

                  mylistiterator& operator++()
                  {
                  node = node->next;
                  return *this;
                  } // no copy made

                  mylistiterator operator++(int)
                  {
                  myiterator ret = *this; // first copy
                  node = node->next;
                  return ret; // second one, usually optimized away
                  }
                  private:
                  mylist::node* node;
                  };

                  So it stands 0 to 2 copies for postfix++ vs. 0 for prefix++.

                  Comment

                  • Method Man

                    #10
                    Re: ++u or u++ which is faster?

                    > > The simple answer is when using U++, the object U needs to get copied[color=blue][color=green]
                    > > twice whereas with ++U, it need only be copied once.[/color]
                    >
                    > What would that extra copy be needed for? I can see U++ doing at most 2
                    > copies (but usually one - or if the function gets inlined and the return
                    > value is not used even both - of them getting optimized away)[/color]

                    One time to store the old value, and another time for returning the old
                    value. Of course any optimizations are compiler-specific, dependent on
                    things like inlining, register variables, etc, or dependent on the
                    ++operator code if U is a user type.

                    But I was speaking theoretically.
                    [color=blue]
                    > and ++U doing
                    > none at all.
                    > ...[/color]

                    You're probably right here.

                    I guess the bottom line is U++ is never faster than ++U.


                    Comment

                    • JKop

                      #11
                      Re: ++u or u++ which is faster?

                      Simon Stienen posted:
                      [color=blue]
                      > Ioannis Vranos <ivr@guesswh.at .grad.com> wrote:[color=green]
                      >> JKop wrote:
                      >>[color=darkred]
                      >>> ++u is faster.[/color]
                      >>
                      >>
                      >> For built-in types their speed is the same. For user defined types it
                      >> applies what you say.[/color]
                      >
                      > So lets state:
                      > ++u is never slower.
                      >
                      > Now it's perfect ;P[/color]

                      Unless ofcourse I play Devil's Advocate and write my own unique ++u
                      operator!

                      -JKop

                      Comment

                      • Pedro Miguel Carvalho

                        #12
                        Re: ++u or u++ which is faster?


                        "The Doctor" <doctor@nospam. com> wrote in message
                        news:Xns956FD71 947B8AdocwhoATb igpondDOTne@61. 9.191.5...
                        | Hi all,
                        |
                        | Recently I was asked the question about whether ++U or U++ is
                        | faster if U is a user defined type. What's the answer?
                        |
                        | Thank you

                        The answer is that it depends on those two operator in the user defined
                        type. ++U is usually faster.

                        Does any one know why these two operator are not symmetrical?

                        UserType& operator++ ( ); // prefix ++
                        UserType operator++ (int dummy); // postfix ++

                        Why can't it be:

                        UserType& operator++ (int dummy); // postfix ++

                        What is the rational behind this?

                        Thanks,
                        Pedro Carvalho



                        Comment

                        • JKop

                          #13
                          Re: ++u or u++ which is faster?

                          > Does any one know why these two operator are not symmetrical?[color=blue]
                          >
                          > UserType& operator++ ( ); // prefix ++
                          > UserType operator++ (int dummy); // postfix ++
                          >
                          > Why can't it be:
                          >
                          > UserType& operator++ (int dummy); // postfix ++
                          >
                          > What is the rational behind this?
                          >
                          > Thanks,
                          > Pedro Carvalho[/color]


                          int& Blah()
                          {
                          int k = 7;

                          return k;
                          }


                          If you don't see the problem in the above then I'd be surprised that you
                          know about operator overloading in the first place. (Not an insult, just an
                          observation.)

                          But ofcourse, you can always:


                          int& Blah()
                          {
                          return *new int(7);
                          }



                          just as long as you call delete down the road...


                          -JKop



                          -JKop

                          Comment

                          • David Hilsee

                            #14
                            Re: ++u or u++ which is faster?

                            "Pedro Miguel Carvalho" <Pedro_MCLX@sap o.pt> wrote in message
                            news:41571136$0 $4790$a729d347@ news.telepac.pt ...[color=blue]
                            >
                            > "The Doctor" <doctor@nospam. com> wrote in message
                            > news:Xns956FD71 947B8AdocwhoATb igpondDOTne@61. 9.191.5...
                            > | Hi all,
                            > |
                            > | Recently I was asked the question about whether ++U or U++ is
                            > | faster if U is a user defined type. What's the answer?
                            > |
                            > | Thank you
                            >
                            > The answer is that it depends on those two operator in the user defined
                            > type. ++U is usually faster.
                            >
                            > Does any one know why these two operator are not symmetrical?
                            >
                            > UserType& operator++ ( ); // prefix ++
                            > UserType operator++ (int dummy); // postfix ++
                            >
                            > Why can't it be:
                            >
                            > UserType& operator++ (int dummy); // postfix ++
                            >
                            > What is the rational behind this?[/color]

                            ++foo should increment foo and return foo. On the other hand, foo++ should
                            increment foo and return the value it had before it was incremented. It
                            should not return a reference to itself, because that would be confusing.
                            It should not return a reference to another instance of UserType because it
                            causes the problems described in JKop's post. It simply can't return a
                            reference without causing complications.

                            --
                            David Hilsee


                            Comment

                            • assaarpa

                              #15
                              Re: ++u or u++ which is faster?

                              > For built-in types their speed is the same. For user defined types it[color=blue]
                              > applies what you say.[/color]

                              Not necessarily, the architechture could be register starved and ++u might
                              fit the computation in the ALU registers while ++u might not, on the other
                              hand this might not have any real-world impact on the performance as the ISA
                              might be translated dynamically to format the hardware ALU uses internally,
                              which may result in same transistors having the same inputs in the same
                              sequence. IA32 is a prime example of architechture where this evaluation has
                              a good chance of being realized.

                              Some architechtures even make distinction of post- and pre-
                              increment/decrement in their binary instructions, think of Motorola 680x0 as
                              one example. It might make a difference or it might not, that depends on the
                              context. As far as the programming language c++ is concerned it does appear
                              to make a difference.


                              Comment

                              Working...