GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 426 of 453    Print  
Does ++p faster than p++?

  
Total Answers and Comments: 3 Last Update: November 07, 2008     Asked by: janhavibend 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Jaya_Kaja
 

Between ++p & p++ , there is no diff in terms of speed of execution .because both are of unary . But there is a diff in the evaluation of an expression by using these unary operations.

++p - First the value of p gets incremented by one & then the incremented value will be used in the expression for further evaluation.
p++ - First the value of p is used in the expression & then the value of p will get incremented by one .

For eg :

a) p= 10;
val = ++p * 100 ;
Here val = 1100;
p = 11;

b) p=10;
val = p++ * 100;
Here val = 1000;
p =11;

- Jayalakshmi Kaja



Above answer was rated as good by the following members:
sourabh_t, Rakesh GangwaR, rajani_vaddepalli15, PThanga_Raj
July 21, 2008 04:53:13   #1  
decentswati Member Since: July 2008   Contribution: 1    

RE: Does ++p faster than p++?

++p is faster as the implementation of p++ will have extra execution cycles for returning the previous memory location reference in addition to the incrementing of value.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 28, 2008 14:47:28   #2  
Jaya_Kaja Member Since: July 2008   Contribution: 3    

RE: Does ++p faster than p++?

Between ++p & p++ there is no diff in terms of speed of execution .because both are of unary . But there is a diff in the evaluation of an expression by using these unary operations.

++p - First the value of p gets incremented by one & then the incremented value will be used in the expression for further evaluation.
p++ - First the value of p is used in the expression & then the value of p will get incremented by one .

For eg :

a) p 10;


val ++p * 100 ;

Here val 1100;

p 11;

b) p 10;
val p++ * 100;
Here val 1000;
p 11;

- Jayalakshmi Kaja


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 1Overall Rating: +1    
November 07, 2008 11:26:25   #3  
sandyseaweed Member Since: November 2008   Contribution: 1    

RE: Does ++p faster than p++?
speed is the same.

int iSpeedTest i++;
00413773 mov eax dword ptr [ebp-0Ch]
00413776 mov dword ptr [ebp-150h] eax
0041377C mov ecx dword ptr [ebp-0Ch]
0041377F add ecx 1
00413782 mov dword ptr [ebp-0Ch] ecx

iSpeedTest ++i;
00413785 mov eax dword ptr [ebp-0Ch]
00413788 add eax 1
0041378B mov dword ptr [ebp-0Ch] eax
0041378E mov ecx dword ptr [ebp-0Ch]
00413791 mov dword ptr [ebp-150h] ecx

 
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