Is this true.. ++i better than i++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Is this true.. ++i better than i++

  1. #1
    Join Date
    Mar 2003
    Posts
    97

    Is this true.. ++i better than i++

    Postfix increment and decrement operators create a temporary object when used while prefix operators donít.

    It is therefore more efficient to use the prefix version where possible, particularly in for loop constructs as whatever is used will be called multiple times in that case, i.e.,

    we should use:

    for (int nType = 0; nType < MM_NUM_TYPES; ++nType)
    {
    Ö
    }

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Is this true.. ++i better than i++

    Originally posted by zoltan_ie
    Postfix increment and decrement operators create a temporary object when used while prefix operators donít.
    Yes, prefix operator are faster. If there is no need to use the original value, it is better to use prefix operator.

  3. #3
    Join Date
    Mar 2003
    Posts
    97
    What I can't understand is why wasn't I told in college? I had to get into my first job to find that out. Also I've never seen it in use, how come?

    any other things I should know about?

  4. #4
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    Hello,

    In addition to the good answer so far, a few additional remarks can be made.

    In loop constructions using integral counters, then all good compilers will put the loop counter in a register or on the stack. The counter will be properly initialized and incremented once upon every trip through the loop in each case at the end of the loop before the assembler instruction for the loop-end comparison. For these simple cases, there will be no performance differences between the two incrementing orders.

    However, for complex types with specialized pre-increment and post-increment operators, the performance issues can be significant.

    Sincerely, Chris.

    You're gonna go blind staring into that box all day.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,569
    The FAQ has a good summary of what has already been said:

    http://www.codeguru.com/forum/showth...hreadid=231052

    A discussion can also be found in one of Scott Meyers
    "Effective C++" books. (I don't remember if it was the
    first or second one).

  6. #6
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    the two operators are usually defined like this:

    Code:
    class c
    {
    c& operator++() // prefix
    {
    m_int++;
    return *this;
    }
    
    c operator++(int) // posfix
    {
    c old = *this;
    m_int++;
    return old;
    }
    
    int m_int;
    };
    So you can see that postfix has two copies and the prefix has none, so obviously it will be slower.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471
    Small point: if you implement postfix ++ (and --) in terms of prefix, you only need to make modifications in one place:
    Code:
    c operator++(int) // posfix
    {
        c old = *this;
        operator++();
        return old;
    }
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center