Is i++ really faster than i = i + 1?

Anyone asking this question does mean that they are simple interested or curious to learn programming. And most of the people will answer i++ is a single instruction where as i = i + 1 contains addition and followed by assignment so i++ is faster. And some people will answer both are same, just for readability purpose we use incremental notation.
Any good compiler will and should generate identical code for i++, i += 1 and i = i + 1. Compilers are meant to optimize code. The programmer should not be bother about such things. Also, it depends on the processor and compiler you are using. One needs to check the compiler’s assembly language output, to see which one of the different approaches are better, if at all.

Program 1:

Let’s take a simple C program with i++ as follows:

Now examine the assembly conversion of the above program

Program 2:

Let’s take an another C program with i = i + 1 as follows:

Now examine the assembly conversion of the above program

By examining both the assembly instructions (P1 and P2), the stack operation and the addition operation are exactly same for both the programs. This shows that i++ & i = i + 1 results same in performance.


I personally feel that speed comes with good & well written algorithms and not from such small tricks.

Thank you for reading this post. Your comments and feedback are always welcome.

7 / 21
WordPress Theme built by Shufflehound. Made with Complete Passion