On 11 Juni, 14:28, anuradhathakur...@gmail.com wrote:
Is there any difference between i++ & i=i+1...
I mean, I read somewhere that i++ is faster then i=i+1..
But as both of them is performing same operations....how can one be
faster than another...
In effect there's no difference, and on a good compiler there should
be none in efficiency either. However I think you confused it with the
question of ++i vs i++, where there's a difference. In the case of i++
the value returned is i before the increment whereas ++i will return
the value of i *after* the increment. This means that i++ must make a
copy of the old value before incrementing and then return the copy.
Most compilers can probably optimize away the difference if the result
is not assigned, at least for built-in types.
When using iterators on the other hand there can be a more noticable
difference since an iterator is larger so more have to be copied, for
that reason ++i is generally preferred when just incrementing.
--
Erik Wikström