Is there a performance difference between i++ and ++i in C?

前端 未结 14 1114
遥遥无期
遥遥无期 2020-11-22 10:08

Is there a performance difference between i++ and ++i if the resulting value is not used?

14条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 10:40

    @Mark Even though the compiler is allowed to optimize away the (stack based) temporary copy of the variable and gcc (in recent versions) is doing so, doesn't mean all compilers will always do so.

    I just tested it with the compilers we use in our current project and 3 out of 4 do not optimize it.

    Never assume the compiler gets it right, especially if the possibly faster, but never slower code is as easy to read.

    If you don't have a really stupid implementation of one of the operators in your code:

    Alwas prefer ++i over i++.

提交回复
热议问题