a friend told me that using pre increment is faster than post increment in for loops. Is this true? If so, then why?
Printable View
a friend told me that using pre increment is faster than post increment in for loops. Is this true? If so, then why?
Just mentioned it in the other thread...
Quote:
Originally Posted by HighCommander4
It could be faster, it could also be slower, most likely it's just going to be equally fast. It depends on the code preceding/following the increment.
Either way, we are talking a very very absolute minimal amount of change in speed (if any), maybe 1ns (yes, 'nano', not milli :D) at most.
The short answer: Don't worry about it.
The long answer: Both ++i and i++ will most likely be compiled into an 'INC <register>' type instruction. Any speed improvements gained would be a result of what precedes or follows the INC instruction.
Who tried (and succeeded) to make you believe something as ridicilous as this claim ? :DQuote:
Originally Posted by HighCommander4
A scenario like this might be true for a class with an overloaded ++ operator (depending on how the overload was written), but it certainly isn't true for int's no temporary variable is needed.
++i : increments i, then returns the incremented value
i++ : returns i, then increments i after whatever the return value was used for.
The only reason why a preincrement is sometimes better than a postincrement is to guarantee a result when more than one increment happens on the same variable during a single compound statement as in:
Behaviour is undefined in this case... It may seem strange, but when compiled in VC++ the above will call foo with the first parameter being 1, the second being 0. i will be 2 at the end regardless.Code:int i=0;
foo(i++,i++);
This above is the C view. In C++ the ++ and -- operators can be overloaded and when they are the postfix forms are inherently slower (because a temporary variable has to be introduced in the overload function). You can as well make a habit of always using ++i (instead of i++) when it doesn't matter to you for some other reasons.Quote:
Originally Posted by OReubens
So in C++ the short answer is: Prefer the prefix forms of ++ and --.
Well, I guess it was Hurb Sutter that made this ridicilous claim :D according to Hurb it's even true for ints. You might want to look atQuote:
Originally Posted by OReubens
this .
Hope this helps,
Regards,
Usman.
How would preincrement help in this case? It's still undefined behaviour, whether you preincrement, postincrement or do a mixture of the two.Quote:
Originally Posted by OReubens
Yes, like I said before, with classes I would agree that usually a postincrement can be made more optimal. But it all depends on how the class operator is written.Quote:
Originally Posted by _uj
However the question here was 'i++' vs '++i', which means incrementing an int. and in that respect both are equally fast.
In fact, the postincrement will usually be slightly faster than preincrement because of the way the CPU reacts to an address/index being changed prior to it being used.
The increment refered to in this instance sample is an increment on an iterator class. And this may indeed be slower. when overloading operators. anything can happen. The ++ operator isn't even required to increment anything. It may just as wel be an 'abuse' to achieve some other effect on the class.Quote:
Originally Posted by usman999_1
On an int it is most likely not to make a difference. Although I have had cases where post incrementing an int yielded a slightly better result than pre incrementing. In fact. I have never had to change a postincrement (on an int) into a preincrement to get better code, while I have had to do the reverse on several occasions. But the cases are rare anyways.
:o erhm. well. erhm. You got me there... to an extent...I guess...Quote:
Originally Posted by Graham
While behaviour is defined in this case (according to my interpretation), it wasn't exactly the behaviour I was expecting either. Both parameters to foo() will be 2 (I was expecting 1,2 at first).
This behaviour does seem to be consistent for the 6 compilers I tried. While the postincrement got me different results... I got these:
0,0
1,0
0,1
Then again, I never write code like this anyway. I have this absolute horror of C/C++ code where an attempt was made to write as few code as possible and try to have a single compound statement do as much as possible.
Some programmers actually expect the compiler to produce better code for this too! While it may have been true in the early days of the C compilers, it no longer holds true for the optimizing ones.
All it really does is make the code less readable, and more difficult to debug.
The Standard says this (Section 5, para 4)Any attempt to increment the same variable twice between sequence points is undefined behaviour.Quote:
Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.
Ok, so it's undefined as well... :DQuote:
Originally Posted by Graham
It's another thing I can add to the list of items in the "avoid complex code" 'war' we have to repeat almost each time we get a new programmer on the team.
How do you know i is an int? That's something you're presuming only.Quote:
Originally Posted by OReubens
It's quite natural presumption in context...
BTW, is the behaviour of something like
int a=(5,6,7);
predictable? I thought that all lists separated by comma are being actualizing right-to-left, so when using function with parameters containing, say, other functions calls, those calls would be preceeded in right-to-left order. It seems now that there is no such rule...
Re FAQ
Why is it necessary to retain a temporary copy of the original variable ?Quote:
The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference.
It's not necessary if you don't mimic the functionality of the built in ++ and -- operators while overloading them. But if you do you'll find that the postfix operator requires you to temporarily store the old value likeQuote:
Originally Posted by Sahir
You don't have to store the old value in the prefix counterpart.Code:T T::operator++(int) { // generic postfix ++ overloading
T old(*this); // store old value
// increment *this
return old; // return old value
}
Ah! To return the old value? I see now. Thanks.
Well, I always prefer preincrement over post increment, unless the program logic dictates otherwise. One reason is that the code is consistent, irrespective of the datatype. And proving a point in C++ by pointing at the assembly code generated by a compiler seems redicilous to me. As then before writing anything one would have to look at the code generated by the compiler, God knows how many C++ compilers exits now (not to mention different versions :)). And last but not least the Gurus prefer preincrement over postincrement.Quote:
Originally Posted by OReubens
Kind Regards,
Usman.
If it's a choice betweenQuote:
Originally Posted by usman999_1
for(int j = 0; j < MAX; j++)
or
for(int j = 0; j < MAX; ++j)
It is merely a matter of style. If your choice is due to the reason quoted here I would like to point out that Bjarne Stroustrup prefers
for(int j = 0; j < MAX; j++)
You can see evidence of this preference in some of his papers.
http://www.research.att.com/~bs/papers.html
For (most) non-fundamental types ++i will be more efficient than i++.
You know old habits die hard, and it would be very easy to increment a std::map iterator using post-increment operators if that is what you do all the time. (Men are by nature creatures of habit.) For this reason alone, I have seen many people suggest that the pre-increment operator always be used -- even for fundamental types -- unless there is specific need for the post-increment operator. I find it difficult to argue with such logic.
for ( int i = 0; i < 5; i++ )
;
So, I have looked what the compiler generate as assembly code. Well, it is the same. Independend of using PRE or POST INC. (Microsoft VC++)
But it's real: for Objects it depends on implementation. And like all other say here, NORMALLY you need for POST INC an temporary variable as return.
And for big objects with memory allocation etc. its then better to use PRE INC. to avoid such memory allocations.
I've even seen a names change to ++C being suggested. :)Quote:
Originally Posted by KevinHall
That's funny! :lol:Quote:
Originally Posted by _uj