GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 198 of 203    Print  
Register Relation
Why p++ is faster than p+1?


  
Total Answers and Comments: 6 Last Update: October 08, 2009     Asked by: akshay201989 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: kumarabhijit
 
I don't think  p+1 is faster than p++.
Internally P++ will be implemented as P+1 only.
Although the ++P is faster than P++ as it avoids the temporary variable creation.

Above answer was rated as good by the following members:
kvkrishnaprasad, taramasalata, ladyscifi, aniketamrutkar, a_l_soni, j_l_larson, MercurySystemsEngineering
May 29, 2009 06:39:47   #1  
kumarabhijit Member Since: May 2009   Contribution: 1    

RE: Register Relation
I don't think p+1 is faster than p++.
Internally P++ will be implemented as P+1 only.
Although the ++P is faster than P++ as it avoids the temporary variable creation.

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 1Overall Rating: +1    
June 13, 2009 01:08:22   #2  
tewari2312 Member Since: June 2009   Contribution: 4    

RE: Register Relation
p++
when the command p++ is given the compiler understands that value of p is incresed by one and wherever p is used (as in the expression u p/2) it(compiler) uses value p+1rather than p so
if the code is
int p;
p++;
float u p/2;
then the lines compiler understands is
int p;
p++;
float u (p+1)/2;

p p+1
when this statement is written then the compiler goes to the location where p is stored and increments the value by 1

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
July 01, 2009 06:28:19   #3  
sahasranaman Member Since: July 2009   Contribution: 1    

RE: Register Relation

p++ is faster because on compilation it gets translated to three machine
instructions where as p p+1 needs four machine instructions. All machine
instructions take the same time.


For p++ the instructions are:

mov ax

inc ax

mov

ax


For p p+ 1 the instructions would be

mov ax

mov bx 1

add bx

mov

ax


This is why p++ is faster than p+1


 
Is this answer useful? Yes | NoAnswer is useful 5   Answer is not useful 0Overall Rating: +5    
July 15, 2009 20:05:20   #4  
yzesong Member Since: July 2009   Contribution: 20    

RE: Register Relation
I agree with sahasranaman. When you look at assembly level P++ take less instructions than P+1 so P++ is faster I think P++ and ++P are the same.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
July 21, 2009 22:57:10   #5  
MIqbalw Member Since: July 2009   Contribution: 1    

RE: Register Relation
Becuase it is a unary operator
 
Is this answer useful? Yes | No
October 08, 2009 11:52:11   #6  
AlexeyP Member Since: October 2009   Contribution: 4    

RE: Register Relation
I think P++ and ++P are the same

No not the same.

For P++ you have to save value in a temp increment *this then return the saved value.

For ++P you just increment and return -- less work.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape