Does ++p faster than p++?

Questions by janhavibend   answers by janhavibend

Showing Answers 1 - 12 of 12 Answers

Jaya_Kaja

  • Jul 28th, 2008
 

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

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 

kbjarnason

  • Jul 1st, 2010
 

The correct answer is that the language does not define the performance of such operations; how they are implemented, how they are optimized and whether one is faster than the other are implementation-specific details, which may very well change, even radically, from one implementation to the next.

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions