Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing iterators: Is ++it more efficient than it++? [duplicate]

Tags:

c++

iterator

stl

People also ask

Which is efficient in for loop i ++ or ++ i?

According to the Google C++ Style Guide, "when the return value is ignored, the 'pre' form ( ++i ) is never less efficient than the 'post' form ( i++ ), and is often more efficient."

Why is pre increment more efficient?

Pre-increment is faster than post-increment because post increment keeps a copy of previous (existing) value and adds 1 in the existing value while pre-increment is simply adds 1 without keeping the existing value.

What happens if you increment the iterator past the end?

Obviously if the iterator is advanced past the last element inside the loop the comparison in the for-loop statement will evaluate to false and the loop will happily continue into undefined behaviour.


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++.


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" (in optimised, non-debug builds), and both operator++s will be inlined. Since 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".


it++ performs the following operations:

  1. create a copy 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 "slower". However, any decent compiler will optimize this difference out for most defined types. For some user-defined types it can be faster.


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.


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.