Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency of postincrement v.s. preincrement in C++ [duplicate]

Tags:

c++

c++11

I usually think that preincrement is more efficient than postincrement in C++. But when I read the book Game Engine Architecture(2nd ed.) recently, there is a section says that postincrement is prefered than preincrement in for loop. Because, as I quote, "preincrement introduces a data dependency into your code -- the CPU must wait for the increment operation to be completed before its value can be used in the expression." Is this true? (It is really subverted my idea about this problem.)

Here is the quote from the section in case you are interested:

5.3.2.1 Preincrement versus Postincrement

Notice in the above example that we are using C++’s postincrement operator, p++, rather than the preincrement operator, ++p. This is a subtle but sometimes important optimization. The preincrement operator increments the contents of the variable before its (now modified) value is used in the expression. The postincrement operator increments the contents of the variable after it has been used. This means that writing ++p introduces a data dependency into your code -- the CPU must wait for the increment operation to be completed before its value can be used in the expression. On a deeply pipelined CPU, this introduces a stall. On the other hand, with p++ there is no data dependency. The value of the variable can be used immediately, and the increment operation can happen later or in parallel with its use. Either way, no stall is introduced into the pipeline.

Of course, within the “update” expression of a for loop (for(init_expr; test_expr; update_expr) { ... }), there should be no difference between pre- and postincrement. This is because any good compiler will recognize that the value of the variable isn’t used in update_expr. But in cases where the value is used, postincrement is superior because it doesn’t introduce a stall in the CPU’s pipeline. Therefore, it’s good to get in the habit of always using postincrement, unless you absolutely need the semantics of preincrement.

Edit: Add "the above example".

void processArray(int container[], int numElements) {     int* pBegin = &container[0];     int* pEnd = &container[numElements];     for (int* p = pBegin; p != pEnd; p++)     {         int element = *p;         // process element...     } }  void processList(std::list<int>& container) {     std::list<int>::iterator pBegin = container.begin();     std::list<int>::iterator pEnd = container.end();     std::list<inf>::iterator p;     for (p = pBegin; p != pEnd; p++)     {         int element = *p;         // process element...     } } 
like image 272
Jaege Avatar asked Aug 15 '16 00:08

Jaege


People also ask

Which is faster Preincrement and Postincrement?

actually - it depends. Postincrement needs a copy since it preserves the old value. If the type incremented is a complex type (e.g. an iterator) and not a simple type, the preincrement is faster than the postincrement. That is only true of course if you don't need the value before the increment.

Why is Preincrement more efficient?

If the counter is a fundamental type and the result of increment is not used, then it makes no difference whether you use post/pre increment. If the counter is not a fundamental type and the result of the increment is not used and optimizations are disabled, then pre increment may be more efficient.

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

Which one is faster i ++ or i 1 explain?

i++ is faster because they return value before. then increment,maybe.... In practical terms the difference isn't worth the time it takes to discuss it. I++ is faster!


2 Answers

pre-increment introduces a data dependency into your code -- the CPU must wait for the increment operation to be completed before its value can be used in the expression."

Is this true?

It is mostly true - although perhaps overly strict. Pre increment doesn't necessarily introduce a data dependency - but it can.

A trivial example for exposition:

a = b++ * 2; 

Here, the increment can be executed in parallel with the multiplication. The operands of both the increment and the multiplication are immediately available and do not depend on the result of either operation.

Another example:

a = ++b * 2; 

Here, the multiplication must be executed after the increment, because one of the operands of the multiplication depends on the result of the increment.

Of course, these statements do slightly different things, so the compiler might not always be able to transform the program from one form to the other while keeping the semantics the same - which is why using the post-increment might make a slight difference in performance.

A practical example, using a loop:

for(int i= 0; arr[i++];)     count++;  for(int i=-1; arr[++i];) // more typically: (int i=0; arr[i]; ++i;)     count++; 

One might think that the latter is necessarily faster if they reason that "post-increment makes a copy" - which would have been very true in the case of non-fundamental types. However, due to the data dependency (and because int is a fundamental type with no overload function for increment operators), the former can theoretically be more efficient. Whether it actually is depends on the CPU architecture, and the ability of the optimizer.

For what it's worth - in a trivial program, on x86 arch, using g++ compiler with optimization enabled, the above loops had identical assembly output, so they are perfectly equivalent in that case.


Rules of thumb:

If the counter is a fundamental type and the result of increment is not used, then it makes no difference whether you use post/pre-increment.

If the counter is not a fundamental type and the result of the increment is not used and optimizations are disabled, then pre-increment may be more efficient. With optimizations enabled, there is usually no difference.

If the counter is a fundamental type and the result of increment is used, then post-increment can theoretically be marginally more efficient - in some CPU architecture - in some context - using some compiler.

If the counter is not a fundamental type and the result of the increment is used, then pre-increment is typically faster than post-increment. Also, see R Sahu's answer regarding this case.

like image 136
eerorika Avatar answered Oct 08 '22 15:10

eerorika


One point of data from my experience.

Changing a post-increment to a pre-increment of a std::map::iterator in for loops resulted in noticeable savings in a core algorithm at my work.

In general, when icrementing an iterator that is a class, i.e. it is not a pointer, you should notice savings when using the pre-increment operator. The reason for it is that the pre-increment operator function changes the object in place while the post increment operator function usually involves creation of a temporary object.

A pre-increment operator is usually implemented as:

typename& typename::operator++() {    // Change state    ...     // Return the object    return *this; } 

while a post-increment operator is usually implemented as:

typename typename::operator++(int) {    // Create a temporary object that is a copy of the current object.    typename temp(*this):     // Change state of the current object    ...     // Return the temporary object.    return temp; } 
like image 30
R Sahu Avatar answered Oct 08 '22 15:10

R Sahu