CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    Re: is ++i faster than i++?

    Re FAQ

    The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference.
    Why is it necessary to retain a temporary copy of the original variable ?

  2. #17
    Join Date
    Nov 2003
    Posts
    1,405

    Re: is ++i faster than i++?

    Quote Originally Posted by Sahir
    Why is it necessary to retain a temporary copy of the original variable ?
    It's not necessary if you don't mimic the functionality of the built in ++ and -- operators while overloading them. But if you do you'll find that the postfix operator requires you to temporarily store the old value like
    Code:
    T T::operator++(int) { // generic postfix ++ overloading
       T old(*this); // store old value
       // increment *this
       return old; // return old value
    }
    You don't have to store the old value in the prefix counterpart.

  3. #18
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    Re: is ++i faster than i++?

    Ah! To return the old value? I see now. Thanks.

  4. #19
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    Re: is ++i faster than i++?

    Quote Originally Posted by OReubens
    The increment refered to in this instance sample is an increment on an iterator class. And this may indeed be slower. when overloading operators. anything can happen. The ++ operator isn't even required to increment anything. It may just as wel be an 'abuse' to achieve some other effect on the class.


    On an int it is most likely not to make a difference. Although I have had cases where post incrementing an int yielded a slightly better result than pre incrementing. In fact. I have never had to change a postincrement (on an int) into a preincrement to get better code, while I have had to do the reverse on several occasions. But the cases are rare anyways.
    Well, I always prefer preincrement over post increment, unless the program logic dictates otherwise. One reason is that the code is consistent, irrespective of the datatype. And proving a point in C++ by pointing at the assembly code generated by a compiler seems redicilous to me. As then before writing anything one would have to look at the code generated by the compiler, God knows how many C++ compilers exits now (not to mention different versions ). And last but not least the Gurus prefer preincrement over postincrement.
    Kind Regards,
    Usman.

  5. #20
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    Re: is ++i faster than i++?

    Quote Originally Posted by usman999_1
    Well, I always prefer preincrement over post increment...... And last but not least the Gurus prefer preincrement over postincrement.
    If it's a choice between
    for(int j = 0; j < MAX; j++)
    or
    for(int j = 0; j < MAX; ++j)

    It is merely a matter of style. If your choice is due to the reason quoted here I would like to point out that Bjarne Stroustrup prefers
    for(int j = 0; j < MAX; j++)
    You can see evidence of this preference in some of his papers.


    http://www.research.att.com/~bs/papers.html
    Last edited by Sahir; February 14th, 2005 at 10:06 PM.

  6. #21
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: is ++i faster than i++?

    For (most) non-fundamental types ++i will be more efficient than i++.

    You know old habits die hard, and it would be very easy to increment a std::map iterator using post-increment operators if that is what you do all the time. (Men are by nature creatures of habit.) For this reason alone, I have seen many people suggest that the pre-increment operator always be used -- even for fundamental types -- unless there is specific need for the post-increment operator. I find it difficult to argue with such logic.
    Kevin Hall

  7. #22
    Join Date
    Nov 2003
    Posts
    4

    Exclamation Re: is ++i faster than i++?

    for ( int i = 0; i < 5; i++ )
    ;

    So, I have looked what the compiler generate as assembly code. Well, it is the same. Independend of using PRE or POST INC. (Microsoft VC++)

    But it's real: for Objects it depends on implementation. And like all other say here, NORMALLY you need for POST INC an temporary variable as return.
    And for big objects with memory allocation etc. its then better to use PRE INC. to avoid such memory allocations.

  8. #23
    Join Date
    Nov 2003
    Posts
    1,405

    Re: is ++i faster than i++?

    Quote Originally Posted by KevinHall
    For this reason alone, I have seen many people suggest that the pre-increment operator always be used
    I've even seen a names change to ++C being suggested.

  9. #24
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: is ++i faster than i++?

    Quote Originally Posted by _uj
    I've even seen a names change to ++C being suggested.
    That's funny!
    Kevin Hall

Page 2 of 2 FirstFirst 12

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured