Thursday 11 April 2013

Question: What is faster ++i or i++, where i is an interger variable?


Question: What is faster ++i or i++, where i is an interger variable?

 Answer: The answer to this lies in the fact, how they used. With ++i(PreIncrement) the variable is incremented and new value is returned. So it requires one instruction to increment the variable.

In case of i++(post Increment), the old value has to be returned or used in the expression and then the variable is incrememented after the expression is evaluated. Since you need one instruction to save the old value to be used in the expression and other instruction to increment the variable, its comparatively slower.

No comments:

Post a Comment