In C language, Why does n++
execute faster than n=n+1
?
(int n=...; n++;)
(int n=...; n=n+1;)
Our instructor asked
Actually, the reason is that the operator is defined differently for post-fix than it is for pre-fix. ++n
will increment "n" and return a reference to "n" while n++
will increment "n" will returning a const
copy of "n". Hence, the phrase n = n + 1
will be more efficient. But I have to agree with the above posters. Good compilers should optimize away an unused return object.