Ruth Samuel
Answered On : 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).
Login to rate this answer.
GAURAV MODY
Answered On : 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.
Login to rate this answer.
"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
Login to rate this answer.
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;
}
Login to rate this answer.