why n++ executes faster than n+1?

by Vijayaprasad 2010-02-16 10:05:01

The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation.

The question is meaningless since the two aren't equivalent.

n++ modifies n.
n+1 does not.

If the purpose is to increment n by one and n is a register variable, the two are almost the same in processor resources, except that the result of n+1 requires the compiler to allocate a second register for the intermediate result.

Also, the first answer is incorrect. The 'INC EAX' instruction in an x86 CPU is no faster than an 'ADD EAX,1' instruction and the only benefit is to reduce code space.

Tagged in:

1673
like
0
dislike
0
mail
flag

You must LOGIN to add comments