CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: i++ vs ++i

  1. #1
    Join Date
    Sep 2009
    Posts
    1

    i++ vs ++i

    Hello all.

    Just a quick question: I read somewhere that ++i is faster in a for loop than i++.

    Why is this the case?

    Thanks in advance for the answers.

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: i++ vs ++i

    i++ increments i and returns the old value of i -> it has to keep the old value somewhere.
    Kurt

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: i++ vs ++i

    For primitives it probably doesn't matter.

    For class types, such as STL iterators, there may be an extra copy required for the iter++ form. The compiler may or may not be able to optimize this out, but it doesn't hurt to use the faster form anyway.

  4. #4
    Join Date
    Aug 2009
    Posts
    10

    Red face Re: i++ vs ++i

    Optimizing compilers make this argument irrelevant.

    Besides, if your speed issue is with incrementation, then your the best C/C++ programmer I've ever seen....lol.

    norman graham
    C++ programmer for 20 years.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: i++ vs ++i

    Quote Originally Posted by normbo
    Optimizing compilers make this argument irrelevant.

    Besides, if your speed issue is with incrementation, then your the best C/C++ programmer I've ever seen....lol.
    The point is, when the expressions are standalone, "it doesn't hurt to use the faster form anyway" since there is no other difference to consider (they are both equally readable and require the same amount of effort to type).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: i++ vs ++i

    As a good rule of thumb, I always prefer prefix to postfix in C++.
    http://www.parashift.com/c++-faq-lit...html#faq-13.15
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: i++ vs ++i

    Quote Originally Posted by normbo View Post
    Optimizing compilers make this argument irrelevant.
    No it doesn't. It all depends on the object that is pre/post incremented. Someone could easily write a custom iterator that has post-increment internally doing something that pre-increment doesn't do, and the compiler cannot optimize it away since it would not reflect what the caller really wants to do.

    The pre and post increment operations are allowed to have side-effects. In this case, the compiler may not be able to optimize the code, and the programmer is responsible for explicitly calling the correct increment operation.

    The only optimization where side-effects are a no-no are the return value optimization (RVO, NRVO), since these optimizations are explicitly specified by the ANSI C++ as perfectly valid, and you shouldn't write code that relies on how many and where copies are being done.

    Regards,

    Paul McKenzie

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