Which do you use, a++ or ++a?
By that I mean where it doesn't matter if it's pre or postincrement.
Printable View
Which do you use, a++ or ++a?
By that I mean where it doesn't matter if it's pre or postincrement.
a++, easier to read, and I'm pretty sure there is no performance issues with it, cuz the return value isn't used.
Herb Sutter suggests..
From guru of the week 5/10 Temporary objects.Quote:
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!
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.
>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.
Take this with a grain of salt because they don't give a good explanation of why but...pg 101 C++ How to Program Deitel & DeitelQuote:
Performance Tip 2.5 Preincrement and predecrement operate slightly faster than postincrement and postdecrement.
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
a++;
If you are looking to optimize your program by optimizing loop counter performance, you are looking at the wrong end.
Indeed. :)Quote:
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.
As a matter of personal taste, I generally use preincrement.
I think ++a will be the best. Because a++ have sometimes a little different in looping. I have never got trouble with ++a.
>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.
a++, because that's how i've learned it.
Quote:
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...
>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.
Indeed. :)
well, both have different uses, for example
whileCode:
int a=5;
cout<<a++;
//would output the number 5 and increment a by one afterwards, so a would now equal 6
Code:int a=5;
cout<<++a;
//would output the number 6 because it would increment a by one before the output.
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++;