tags:

views:

784

answers:

8

Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?

I am writing a program where an iterator is used to loop through a std::vector. Somebody told me that doing ++it in the for statement leads to more efficient code. In other words, they are saying that:

for ( vector<string>::iterator it=my_vector.begin(); it != my_vector.end(); ++it )

runs faster than

for ( vector<string>::iterator it=my_vector.begin(); it != my_vector.end(); it++ )

Is this true? If it is, what is the reason behind the efficiency improvement? All it++/++it does is move the iterator to the next item in the vector, isn't it?

+2  A: 

Sometimes yes. With some it will be optimized away and be the same. For std::vector<> (and other std-iterators) it will most likely be optimized to be the same.

Thomas
A: 

Yes. As far as I remember, ++it is more efficient than it++, because it++ creates a temporary object, while ++it does not.

Stefano Borini
+1  A: 

yes ++it is more efficient because it++ need to return a copy of the object then increment itself.

oykuo
+7  A: 

The reason behind the preincrement being faster is that post-increment has to make a copy of the old value to return. As GotW #2 put it, "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."

GotW #55 provides the canonical form of postincrement, which shows that it has to do preincrement plus some more work:

T T::operator++(int)
{
  T old( *this ); // remember our original value
  ++*this;        // always implement postincrement
                  //  in terms of preincrement
  return old;     // return our original value
}

As others have noted, it's possible for some compiler to optimize this away in some cases, but if you're not using the return value it's a good idea not to rely on this optimization. Also, the performance difference is likely to be very small for types which have trivial copy constructors, though I think using preincrement is a good habit in C++.

aem
Note that decent compilers will optimize away the copy of the old value if it never gets used.
Novelocrat
Exactly: you can't promise that preincrement is more efficient than postincrement, because (a) the standard does not require it, and (b) it's pretty easy to invent combinations of source code and compiler where the two are equally efficient.
Steve Jessop
But you can assume that preincrement is at least as efficient as postincrement. So why not just always use it, when they are otherwise equivalent?
Nick Lewis
Agreed: I think use preincrement where possible, since I can't think of a legitimate reason for it to be worse than post. If some class overloads both operators (badly) and somehow makes preincrement worse, then the caller can't be blamed for failing to work around it. I just don't think one should promise that it is "more efficient" (which many will read as "strictly more efficient"), when there is no such guarantee, and indeed typically they're identical in optimised code.
Steve Jessop
+1  A: 

Closely related to these other questions:

abelenky
I think the comments are a better place for this. See Shog9's one
Johannes Schaub - litb
+1  A: 

There is a chance that it++ will create a temporary copy.

Also, in C++, there is a chance that someone has overloaded the postincrement operator.

Both of these things could decrease performance vs preincrement. Neither is likely to matter in practice. The temporary copy, in particular, will be optimized away by most compilers since there are no side effects in the 3rd expression of your For loop.

Richard Berg
+2  A: 

It's unlikely to make any difference for a vector.

In general, ++it is extremely unlikely to be slower than it++ (assuming a sensible implementation, if they're overloaded), and just might be faster. The reason is that if the iterator class itself is at all complex, then because it++ has to return the value before it is incremented, the implementation will generally make a copy.

Vector iterators are probably "just pointers", and both operator++s will be inlined, and as the return value is unused the copy will typically be elided. So it won't make any difference. I'm in the habit of typing ++it because:

1) Some day it might make a difference, for some iterator type, and I don't want to have to do something special for that type.

2) Personally I think the prefix operator more clearly expresses the intent: "increment it", as opposed to "use it and then increment".

Steve Jessop
A: 

it++ performs the following operations:

  1. create a copy of of it
  2. increment it
  3. return the original (non-incremented) it

++it performs the following operations:

  1. increment it
  2. return it

Because it++ creates a copy, it can be said to be "faster". However, any decent compiler will optimize this difference out for most defined types. For some user-defined types it can be faster.

thekidder