Feb 27, 2010

Understanding which is faster Post or Pre increment operator ( i++ vs ++i ) ?

In c++ pre and post operator are used with variables where one need to just increment or decrement value by 1.For incrementing value ++ operator can be used and for decrementing -- operator can be used.
Its use is very simple one can easily say ++i or i++, to increment its value by 1. Many a times i found people very confused about it's usage (at beginners level) and some professional showing lots of concerns about its performance .That's what made me writing this post.
lets focus on its use first:-
One can now ask question that why there are two different version present for if we simply need to increment or decrement the value.
Lets take one example to see its effect in expression for post and pre increment operator :-

a) x = ++y; //pre increment
b) x = y++;//post increment

For above two expression value of x will be different. In expression (a) value of y would be incremented first then it would be assigned to x. And in expression (b), x would be assigned value of y first and then y would be increment. So if the value y is 5, then value of x would be 6 in expression (a) and its value would be 5 in expression (b).




We are now aware of its usage and impact in an expression, now lets find out which is better ?
To a much extent we can say that pre-increment is faster then post-increment operator. If we talk about independent statement containing just pre-increment and post-increment operator then pre-increment is faster (without compiler optmization).but, if we talk about integer variable the difference is almost negligible.It would be one or two cycle in unoptimized c++ code.As with pre-increment operator we did not need temporary location to store value, value is directly incremented and assigned where as in post-increment operator we need to store value in temporary variable. So with data type like integer, pointers no major performance can be achieved but if we talk about classes one should prefer use pre-increment operator.
Lets try to implement both operator it would help us gain understanding about it :-
pre-increment operator
++i
int PreIncrement(int i)
{
  i = i+1;//incrementing i
return i;
}

post-increment operator

i++
int PostIncrement(int i)
{
  int tempvar = i;//temporary storage
  i = i + 1;//incrementing i
  return tempvar; //return tempvar
}


With above example its very clear that pre-increment is better and efficient then post-increment operator.
Again i want to make one point clear that it would not improve performance for data type like integers, pointer with advanced compiler you will not see any difference in assembly code too.I have written one test code to show this, lets look at it :-



int x =0;
001F361C  mov         dword ptr [x],0 
int y =1;
001F3623  mov         dword ptr [y],1 
x = ++y;
001F362A  mov         eax,dword ptr [y] 
001F362D  add         eax,1 
001F3630  mov         dword ptr [y],eax 
001F3633  mov         ecx,dword ptr [y] 
001F3636  mov         dword ptr [x],ecx 
x = y++;
001F3639  mov         eax,dword ptr [y] 
001F363C  mov         dword ptr [x],eax 
001F363F  mov         ecx,dword ptr [y] 
001F3642  add         ecx,1 
001F3645  mov         dword ptr [y],ecx 
y++;
001F3648  mov         eax,dword ptr [y] 
001F364B  add         eax,1 
001F364E  mov         dword ptr [y],eax 
++y;
001F3651  mov         eax,dword ptr [y] 
001F3654  add         eax,1 
001F3657  mov         dword ptr [y],eax 


With above disassembly code its clear that is no much impact on assembly instruction, code generated is almost same for both the cases.With all the analysis done so far, i am still thinking about post-increment operator.All points are going in-favor of pre-increment operator but there should be some benefit of post-increment operator too, where it can be used. Finally i found one such condition where post increment is better then pre-increment operator, lets look at following example :-
a) x = arrIntValues[i++]
b) x = arrIntValues[++i]

In above case expression (a) is better than (b) because in the second case,the address calculation  of the array element has to wait for the new value of i which will delay the availability of x for one or two clock cycles.But it should be used cautiously since if you are making such changes in programs it may change code behavior.

Time to conclude now. As we have seen usage and comparison of pre/post increment operator, which shows that both are equally good if used for primitive data types with optimized compiler option.For non primitive data type one should use pre-increment operator. And finally it depends upon your code condition too, as we have seen one condition where post-increment is more efficient then pre-increment operator.Whole discussion    is also applicable for pre/post decrement operator too.

Hope you enjoyed the post, Cheers,
-Tajendra




2 comments:

Repairing of Lenovo laptop are very well done by us as we have experts that are well versed with the technology used in Lenovo laptop to build them. Get in touch with us for swift & skilled help in repairing your Lenovo laptop system. Lenovo service center in Gurgaon. Call us: 7217871051
Lenovo Service Center in Gurgaon

Repairing of Lenovo laptop are very well done by us as we have experts that are well versed with the technology used in Lenovo laptop to build them. Get in touch with us for swift & skilled help in repairing your Lenovo laptop system. Lenovo service center in Delhi. Call us: 7217871051
Lenovo Service Center in Delhi

Post a Comment