GeekInterview.com
Series: Subject: Topic:
Question: 311 of 357

Why preincrement operator is faster than postincrement?

Asked by: Interview Candidate | Asked on: Aug 30th, 2005
Showing Answers 1 - 4 of 4 Answers
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=5e++;     on this line value of e=5++e;     on this line value of e=7.gaurav mody.

  
Login to rate this answer.
Freighter

Answered On : Sep 17th, 2009

View all answers by Freighter

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

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.