Why preincrement operator is faster than postincrement?

Showing Answers 1 - 4 of 4 Answers

Ruth Samuel

  • Aug 30th, 2005
 

Preincrement can be faster on systems where the increment can not begin until the comparison is complete -such as a Pentium. The P6 core can use register aliasing to start the increment (using 'load effective address' so as not to ruin the comparison test).

  Was this answer useful?  Yes

GAURAV MODY

  • Dec 30th, 2006
 

the pre inc operator increments the value of that particular variable on that line itself

the post inc operator increments the value of that particular variable  after going on the next line

in simple words

eg.

e=5;    on this line value of e=5

e++;     on this line value of e=5

++e;     on this line value of e=7.

gaurav mody.

  Was this answer useful?  Yes

Freighter

  • Sep 17th, 2009
 

"Postincrement is usually less efficient than preincrement because it has to remember & return its original value"

That is, apart from also doing the increment operation, Postincrement has to remember & return its original value.

Exceptional C++, Herb Sutter, Item #6, p19

  Was this answer useful?  Yes

Post Increment / Decrement is always slower and costlier than their Pre forms (Pre Inc- or Pre Decrement).


Both do Increment / Decrement, but in case of Post, it has to return the OLD value of the variable / object.
So, another instruction for CPU to retain old value, and another local variable is required to hold old value.

Its like (just an ex in crude ways):
Pre:
int& preInc(int& x)
{
x=x+1;
return x;
}

post:
int& postInc(int& x)
{
int y = x;
x=x+1;
return y;
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions