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

前端 未结 14 1097
遥遥无期
遥遥无期 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:43

    I always prefer pre-increment, however ...

    I wanted to point out that even in the case of calling the operator++ function, the compiler will be able to optimize away the temporary if the function gets inlined. Since the operator++ is usually short and often implemented in the header, it is likely to get inlined.

    So, for practical purposes, there likely isn't much of a difference between the performance of the two forms. However, I always prefer pre-increment since it seems better to directly express what I"m trying to say, rather than relying on the optimizer to figure it out.

    Also, giving the optmizer less to do likely means the compiler runs faster.

提交回复
热议问题