View Poll Results: a++ or ++a?

Voters
35. You may not vote on this poll
  • a++

    23 65.71%
  • ++a

    10 28.57%
  • a = a + 1

    1 2.86%
  • a = 1 + a

    0 0%
  • never uses them

    1 2.86%

Thread: a++ or ++a

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    a++ or ++a

    Which do you use, a++ or ++a?
    By that I mean where it doesn't matter if it's pre or postincrement.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    a++, easier to read, and I'm pretty sure there is no performance issues with it, cuz the return value isn't used.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Herb Sutter suggests..
    3. This one was more subtle. Preincrement is more efficient than postincrement, because for postincrement the object must increment itself and then return a temporary containing its old value. Note that this is true even for builtins like int!
    From guru of the week 5/10 Temporary objects.

    Even if there is no return value, I think getting in the habit of using preincrement is not a bad idea for cases when this does apply.
    Last edited by curlious; 11-24-2003 at 04:44 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Note that this is true even for builtins like int!
    Only if you have an incredibly stupid compiler. Even Ritchie's first C compiler managed to optimize away the difference between ++a and a++ as standalone expressions. However, with user defined operators (ie. any class that supports them), you cannot be sure that postincrement is well optimized. In my experience, it usually isn't, but you also cannot be sure that preincrement is optimized either. I agree with Herb Sutter in that you should assume preincrement is more efficient in its use of temporaries when using objects, but with builtin data types it really doesn't matter.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Take this with a grain of salt because they don't give a good explanation of why but...
    Performance Tip 2.5 Preincrement and predecrement operate slightly faster than postincrement and postdecrement.
    pg 101 C++ How to Program Deitel & Deitel

    personally I believe Prelude she seems better informed and I think her post states the situation better than these wrote rules, but this is what us newbies are learning

    That being said I'll probably still use ++a

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    a++;

    If you are looking to optimize your program by optimizing loop counter performance, you are looking at the wrong end.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by nvoigt
    a++;

    If you are looking to optimize your program by optimizing loop counter performance, you are looking at the wrong end.
    Indeed.

    As a matter of personal taste, I generally use preincrement.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189

    Thumbs up

    I think ++a will be the best. Because a++ have sometimes a little different in looping. I have never got trouble with ++a.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Indeed.
    I think you missed the point. A well known micro-optimization technique is to invert the loop, starting from the end and decrementing. This is on the assumption that some machines have a special instruction for decrementing and comparing with zero and a reversed loop is a good hint to the compiler to use those instructions.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Nov 2003
    Posts
    168
    a++, because that's how i've learned it.
    -Felix
    Rots Soft
    If the facts don't fit the theory, change the facts.
    Albert Einstein (1879 - 1955)

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by Prelude
    >Indeed.
    I think you missed the point. A well known micro-optimization technique is to invert the loop, starting from the end and decrementing. This is on the assumption that some machines have a special instruction for decrementing and comparing with zero and a reversed loop is a good hint to the compiler to use those instructions.

    I took nvoigt's post as meaning 'loop counter performance optimizations on a program produce an almost negligible effect', which I would agree with.

    Compilers don't generate perfectly optimized code, if raw speed is what you want, use assembly...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I took nvoigt's post as meaning 'loop counter performance optimizations on a program produce an almost negligible effect'
    It could have been taken any number of ways. nvoigt was being especially cryptic.
    My best code is written with the delete key.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Indeed.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Registered User Scourfish's Avatar
    Join Date
    Mar 2002
    Posts
    387
    well, both have different uses, for example

    Code:
    int a=5;
    cout<<a++;
    //would output the number 5 and increment a by one afterwards, so a would now equal 6
    while

    Code:
    int a=5;
    cout<<++a;
    //would output the number 6 because it would increment a by one before the output.
    -486SX-20
    -Some random Debian Distro
    -Some version of MS-Dos
    -Day of the Tentacle

  15. #15
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    i always use post-increment unless i have to use a pre-increment because of the situation.

    post-increment just looks more natural to me.

    a++;
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed