Why i++ is faster than i=i+1 in c


i=i+1:

To perform this operation compiler has to perform two operations:

(1)ADD      \\i+1
(2)Assignment operation          \i=x

i++:

To perform this operation compiler has to perform only one operation:

(1)INR

Note: above operation are microprocessor instructions i.e. in assembly language
so i++ is faster

5 comments:

sandy said...

Very helpful...........

Anoop said...

Gud :)

Anonymous said...

gr8

Anonymous said...

nice. but internally compiler may use optimization techniques to replace all forms of incrementing the value of i with i++.....

Anonymous said...

type two .c file in linux and then check assembly code
1.c
main()
{
int i =0;
i++;
}
2.c
main()
{
int i =0;
i=i+1;
}
in my system assembly code for both the files are same.
so where is the difference?