Does Pre and post increment/decrement operators in C++ have same performance in a loop? [duplicate]

StackOverflow https://stackoverflow.com/questions/631506

  •  08-07-2019
  •  | 
  •  

Question

Consider following two examples.

class ClassOne
{
 //class definition is here
};

std::vector< ClassOne > myListOfObjects;

std::vector< ClassOne >::const_iterator iter = myListOfObjects.begin();

Example 1: 
for( ; iter < myListOfObjects.end(); **++iter**)
{
   //some operations
}

OR

Example 2: 
for( ; iter < myListOfObjects.end(); **iter++**)
{
   //some operations
}

Which one is faster ? ++iter or iter++ in the context of loop.

Reason Fro Closure:

Copied from Brian's Post (to make the question more succinct).

You could also try one of these similar questions: here or here or here or here.

Was it helpful?

Solution

As noted in this answer, pre is faster. They are only the same when you are dealing with primitives (int, char, etc.). In this case, they are calling overloaded operators.

OTHER TIPS

No they do not have the same performance:

  • Postincrement will create a copy then increment, then return the copy
  • Preincrement will increment then return the original

So (unless you're handling simple things like ints) preincrement is faster.

It depends. In a naive compiler, preincrement will be faster. In pseudocode, here's what each of them does:

preincrement() {
  val = val + 1;
  return val;
}

postincrement() {
 tmp = val; // take a temporary copy of the old value
 val = val + 1;
 return tmp;
}

In practice, the compiler will often turn a postincrmeent into preincrement when it can get away with it. But for complex types, it may not be able to do that. As a general rule, use preincrement whenever you can, and only use postincrmenet when you need the specific semantics of that operation.

When dealing with non primitive types: ++iter will always be more efficient.

The reason ++iter is more efficient comes down to the work each needs to do to obtain their return value. Whether the return value is actually used or not doesn't matter.

The difference:

  • ++iter returns a reference to itself, so there is no temporary copy needed. (increment, return reference to own value)

  • iter++ returns a temporary copy of the variable (create temp var with old value, increment, return temp var)

    _Myt _Tmp = *this;
    ++*this;
    return (_Tmp);
    

You could also try one of these similar questions: here or here or here or here. They are not exactly the same though as they don't apply to iterators.

I'm almost positive they reduce to the same thing in machine code, just in a different order. I'd say they're the same, but I'd recommend you write a quick test app to check.

Pre-increment is, in general, better, provided you don't have a specific need for post-increment.

However, this is a compiler specific thing. Most modern compilers will create fewer ops for a prefix notation. With class method overloads, this doesn't always get optimized, though, since it depends on how the class is implemented.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top