-
February 12th, 2005, 11:39 PM
#16
Re: is ++i faster than i++?
Re FAQ
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.
Why is it necessary to retain a temporary copy of the original variable ?
-
February 13th, 2005, 01:09 AM
#17
Re: is ++i faster than i++?
Originally Posted by Sahir
Why is it necessary to retain a temporary copy of the original variable ?
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 like
Code:
T T::operator++(int) { // generic postfix ++ overloading
T old(*this); // store old value
// increment *this
return old; // return old value
}
You don't have to store the old value in the prefix counterpart.
-
February 13th, 2005, 10:47 PM
#18
Re: is ++i faster than i++?
Ah! To return the old value? I see now. Thanks.
-
February 14th, 2005, 04:53 AM
#19
Re: is ++i faster than i++?
Originally Posted by OReubens
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.
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.
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.
Kind Regards,
Usman.
-
February 14th, 2005, 12:26 PM
#20
Re: is ++i faster than i++?
Originally Posted by usman999_1
Well, I always prefer preincrement over post increment...... And last but not least the Gurus prefer preincrement over postincrement.
If it's a choice between
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
Last edited by Sahir; February 14th, 2005 at 10:06 PM.
-
February 14th, 2005, 02:24 PM
#21
Re: is ++i faster than i++?
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.
Kevin Hall
-
February 16th, 2005, 03:14 AM
#22
Re: is ++i faster than i++?
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.
-
February 16th, 2005, 04:25 AM
#23
Re: is ++i faster than i++?
Originally Posted by KevinHall
For this reason alone, I have seen many people suggest that the pre-increment operator always be used
I've even seen a names change to ++C being suggested.
-
February 16th, 2005, 08:21 AM
#24
Re: is ++i faster than i++?
Originally Posted by _uj
I've even seen a names change to ++C being suggested.
That's funny!
Kevin Hall
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|