Why does n++ execute faster than n=n+1?

前端 未结 10 1017
[愿得一人]
[愿得一人] 2021-01-30 22:05

In C language, Why does n++ execute faster than n=n+1?

(int n=...;  n++;)
(int n=...;  n=n+1;)

Our instructor asked

10条回答
  •  [愿得一人]
    2021-01-30 22:32

    The compiler will optimize n + 1 into nothingness.

    Do you mean n = n + 1?

    If so, they will compile to identical assembly. (Assuming that optimizations are on and that they're statements, not expressions)

提交回复
热议问题