n++ vs n=n+1. Which one is faster
- by piemesons
Somebody asked me Is n++ faster than n=n+1?
My answer:--
++ is a unary operator in C which(n++) takes only one machine instruction to execute while n=n+1 takes more than one machine instructions to execute.
Anyone correct me if I am wrong, but in Assembler it take something like this:
n++:
inc n
n = n + 1;
mov ax n
add ax 1
mov n ax
its not exactli this, but it's near it.but in most cases a good compiler will change n = n + 1 to ++n.So A good compiler will generate same code for both and hence the same time to execute.