-
August 28th, 2003, 05:37 AM
#1
Is this true.. ++i better than i++
Postfix increment and decrement operators create a temporary object when used while prefix operators don’t.
It is therefore more efficient to use the prefix version where possible, particularly in for loop constructs as whatever is used will be called multiple times in that case, i.e.,
we should use:
for (int nType = 0; nType < MM_NUM_TYPES; ++nType)
{
…
}
-
August 28th, 2003, 05:42 AM
#2
Re: Is this true.. ++i better than i++
Originally posted by zoltan_ie
Postfix increment and decrement operators create a temporary object when used while prefix operators don’t.
Yes, prefix operator are faster. If there is no need to use the original value, it is better to use prefix operator.
-
August 28th, 2003, 05:45 AM
#3
What I can't understand is why wasn't I told in college? I had to get into my first job to find that out. Also I've never seen it in use, how come?
any other things I should know about?
-
August 28th, 2003, 06:09 AM
#4
Hello,
In addition to the good answer so far, a few additional remarks can be made.
In loop constructions using integral counters, then all good compilers will put the loop counter in a register or on the stack. The counter will be properly initialized and incremented once upon every trip through the loop in each case at the end of the loop before the assembler instruction for the loop-end comparison. For these simple cases, there will be no performance differences between the two incrementing orders.
However, for complex types with specialized pre-increment and post-increment operators, the performance issues can be significant.
Sincerely, Chris.
You're gonna go blind staring into that box all day.
-
August 28th, 2003, 07:10 AM
#5
The FAQ has a good summary of what has already been said:
http://www.codeguru.com/forum/showth...hreadid=231052
A discussion can also be found in one of Scott Meyers
"Effective C++" books. (I don't remember if it was the
first or second one).
-
August 28th, 2003, 08:43 AM
#6
the two operators are usually defined like this:
Code:
class c
{
c& operator++() // prefix
{
m_int++;
return *this;
}
c operator++(int) // posfix
{
c old = *this;
m_int++;
return old;
}
int m_int;
};
So you can see that postfix has two copies and the prefix has none, so obviously it will be slower.
-
August 28th, 2003, 09:41 AM
#7
Small point: if you implement postfix ++ (and --) in terms of prefix, you only need to make modifications in one place:
Code:
c operator++(int) // posfix
{
c old = *this;
operator++();
return old;
}
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
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
|