Do C and Pre increment / decment statements have the same performance in a loop?

Let's look at 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 the loop.

Reason Closing Fro:

Copied from a Brian Post post (to make the question more concise).

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

+1
c ++ for-loop
source share
6 answers

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

+4
source share

No, they do not have the same performance:

  • Postincrement will create a copy, then enlarge it, and then return the copy
  • Preincrement will increase and then return the original

So (if you don't handle simple things like ints), pre-incrementing is faster.

+7
source share

It depends. In a naive compiler, pre-increment will be faster. In pseudo-code, where each of them:

 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 often turns postincrmeent into preincrement when it can handle it. But for complex types, he may not be able to do this. As a rule, use preincrement whenever you can, and use only postincrmenet when you need the specific semantics of this operation.

+3
source share

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

The reason ++ iter is more effectively reduced to the work that everyone has to do to get the return value. Whether the actual value of the return value or not does not matter.

Difference:

  • ++ iter returns a link to itself , so a temporary copy is not required. (increment, return link to eigenvalue)

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

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

You can also try one of these similar questions: here or here or here or here . They are not exactly the same, although they do not apply to iterators.

+2
source share

I am pretty sure that they come down to the same thing in machine code, only in a different order. I would say that they are the same, but I would recommend that you write a quick test application for verification.

0
source share

Pre-increment is generally better if you do not have a specific need for increment.

However, this is specific to the compiler. Most modern compilers will create fewer prefix operations. When overloading a class method, this is not always optimized, though, since it depends on how the class is implemented.

0
source share

All Articles