PDA

View Full Version : a++ or ++a



Magos
11-24-2003, 04:26 AM
Which do you use, a++ or ++a?
By that I mean where it doesn't matter if it's pre or postincrement.

glUser3f
11-24-2003, 04:37 AM
a++, easier to read, and I'm pretty sure there is no performance issues with it, cuz the return value isn't used.

curlious
11-24-2003, 04:41 AM
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.

Prelude
11-24-2003, 07:07 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.

curlious
11-24-2003, 08:53 AM
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

nvoigt
11-24-2003, 09:12 AM
a++;

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

Sebastiani
11-24-2003, 09:21 AM
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.

afreedboy
11-24-2003, 09:40 AM
I think ++a will be the best. Because a++ have sometimes a little different in looping. I have never got trouble with ++a.

Prelude
11-24-2003, 10:06 AM
>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.

Felix
11-24-2003, 10:10 AM
a++, because that's how i've learned it.

Sebastiani
11-24-2003, 10:24 AM
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...

Prelude
11-24-2003, 10:46 AM
>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.

Sebastiani
11-24-2003, 10:48 AM
Indeed. :)

Scourfish
11-24-2003, 11:55 AM
well, both have different uses, for example




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


while



int a=5;
cout<<++a;
//would output the number 6 because it would increment a by one before the output.

DavidP
11-24-2003, 12:18 PM
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++;

major_small
11-24-2003, 01:13 PM
^ I agree... but please don't say 'natural'... after another thread I started, I plan on completely deleting the word 'natural' from my vocabulary...

Speedy5
11-24-2003, 04:08 PM
How about:

a += 1;

I'm tending to use that more and more often. It just looks better and clearer.

Lurker
11-24-2003, 04:43 PM
Originally posted by Speedy5
How about:

a += 1;

I'm tending to use that more and more often. It just looks better and clearer.

:rolleyes: oookkk....

Zach L.
11-24-2003, 07:32 PM
Unless I am using the return value for something (which is fairly rare) forcing the issue, I prefer to use ++a.

golfinguy4
11-24-2003, 07:46 PM
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.

Yes, but the only difference is a single subtraction operation done by the processor. If you need to optimize things like that, do what Sebastiani said and use asm.

major_small
11-24-2003, 08:41 PM
Originally posted by Speedy5
How about:

a += 1;

I'm tending to use that more and more often. It just looks better and clearer. are you insane? how does
a += 1;look better than
a++;
//or
++a;:rolleyes: i just don't get it...

SourceCode
11-24-2003, 08:47 PM
Originally posted by Speedy5
How about:

a += 1;

I'm tending to use that more and more often. It just looks better and clearer.
I use that only when it's a number other an 1.

For just an incremment of one I usually use a++ out of habit, but sometimes just to be "a little crazy" about things I will use ++a hehe.

Yoshi
11-24-2003, 09:11 PM
C++, there is nothing better than this.

frenchfry164
11-24-2003, 09:19 PM
I wonder if there will ever be a language based on C++ called C+=2

Cat
11-25-2003, 12:00 AM
Hey, I made that joke a few weeks ago :P

I do tend to use ++a (or --a) unless I need the old value. Although any good optimizing compiler should make no distinction, ++a is:

* As readable as a++
* Never worse in performance than a++ and sometimes better

I agree, increment/decrement is not the place to look at optimizing, but this is not your typical optimization, because there is no benefit from a++ if you don't need the old value. Optimization is all about making decisions based on tradeoffs. For example, do you want a fast program or a small one? Do you want to take advantage of processor-specific features that might slow your program down on other systems? Do you want to keep your classes very uncoupled, making code changes easier and bugs rarer, or do you need the added speed that tighter coupling can provide?

In this case, if there is only one side of the story -- if ++a is always as good and possibly better (at least in cases where ++ has the typical meaning), then I think ++a is the way to go.

Perspective
11-27-2003, 09:15 PM
a++ is much faster than ++a because its easier for the 'a' to push the ++ forward in the program rather than pull it. dont you agree that pushing is easier than pulling?

Kinasz
11-27-2003, 10:39 PM
pulling is much easier when your in practise :)

Magos
11-29-2003, 10:46 AM
According to the discussion of whether ++a is faster than a++ I have made a few tests. i compiled the test program below (without optimizations) and got the following results:


EDIT:
Old code, see next page for updated test


As you can see, not much differs from them. They have the same instructions run, only in a different order. My conclusion from this is they are both running at the same speed. If someone else have anything to add, please do.

EDIT: Another observation. In a = a++ there is this line:


mov eax, DWORD PTR _a$[ebp]
mov DWORD PTR _a$[ebp], eax

This could be optimized away, thus making a=a++ faster.

Prelude
11-29-2003, 10:54 AM
>This could be optimized away, thus making a=a++ faster.
Why would you want to optimize undefined behavior?

Zach L.
11-29-2003, 11:04 AM
> Why would you want to optimize undefined behavior?
Well would you want it slow... There's no fun in that. :D

Magos
11-29-2003, 11:18 AM
Originally posted by Prelude
>This could be optimized away, thus making a=a++ faster.
Why would you want to optimize undefined behavior?
Huh? After you've moved it from 'a' to eax, there is no need to move it back from eax to 'a'. It's already there. I didn't mean removing both lines if that's what you hinted ^^.

Prelude
11-29-2003, 11:23 AM
>I didn't mean removing both lines if that's what you hinted ^^.
What I was referring to was the fact that both the C and C++ standards clearly state that modifying a variable more than once between sequence points results in undefined behavior. Thus, a=a++ is incorrect unless your compiler supports it, then it's simply non-portable. If you changed the lines to


a++;

and


++a;

Then your comments would be more correct, though you would have to rebuild the assembly output as it would be different.

Magos
11-29-2003, 11:29 AM
Oh, I see. Didn't think of that. I didn't want to use only a++ and ++a in case the return value would be omitted. Anyway, I redid the test like this (I see now what that double mov-statement was ^^). The result is the same. Both ways have the same instructions set, only in a different order.



void Inc_1()
{
int a = 0;
int b = a++; //Line 4
}

void Inc_2()
{
int a = 0;
int b = ++a; //Line 10
}

int main()
{
Inc_1();
Inc_2();
return 0;
}




; Line 4
mov eax, DWORD PTR _a$[ebp]
mov DWORD PTR _b$[ebp], eax
mov ecx, DWORD PTR _a$[ebp]
add ecx, 1
mov DWORD PTR _a$[ebp], ecx




; Line 10
mov eax, DWORD PTR _a$[ebp]
add eax, 1
mov DWORD PTR _a$[ebp], eax
mov ecx, DWORD PTR _a$[ebp]
mov DWORD PTR _b$[ebp], ecx