In C language, Why does n++ execute faster than n=n+1?
(int n=...; n++;)
(int n=...; n=n+1;)
Our instructor asked that question in today’s class. (this is not homework)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That would be true if you are working on a “stone-age” compiler…
In case of “stone-age”:
++nis faster thann++is faster thann=n+1Machine usually have
increment xas well asadd const to xn++, you will have 2 memory access only (read n, inc n, write n )n=n+1, you will have 3 memory access (read n, read const, add n and const, write n)But today’s compiler will automatically convert
n=n+1to++n, and it will do more than you may imagine!!Also on today’s out-of-order processors -despite the case of “stone-age” compiler- runtime may not be affected at all in many cases!!
Related