Which do you use, a++ or ++a?
By that I mean where it doesn't matter if it's pre or postincrement.
a++
++a
a = a + 1
a = 1 + a
never uses them
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.
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.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.
Last edited by curlious; 11-24-2003 at 04:44 AM.
>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.
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 & DeitelPerformance 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.
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.
Indeed.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.
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; }
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.
My best code is written with the delete key.
a++, because that's how i've learned it.
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; }
>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.
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; }
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.
-486SX-20
-Some random Debian Distro
-Some version of MS-Dos
-Day of the Tentacle
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++;