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

前端 未结 17 2063
臣服心动
臣服心动 2020-11-21 17:15

We have the question is there a performance difference between i++ and ++i in C?

What\'s the answer for C++?

17条回答
  •  遇见更好的自我
    2020-11-21 18:13

    @Ketan

    ...raises over-looked detail regarding intent vs performance. There are times when we want to use iter++ instead of ++iter.

    Obviously post and pre-increment have different semantics and I'm sure everyone agrees that when the result is used you should use the appropriate operator. I think the question is what should one do when the result is discarded (as in for loops). The answer to this question (IMHO) is that, since the performance considerations are negligible at best, you should do what is more natural. For myself ++i is more natural but my experience tells me that I'm in a minority and using i++ will cause less metal overhead for most people reading your code.

    After all that's the reason the language is not called "++C".[*]

    [*] Insert obligatory discussion about ++C being a more logical name.

提交回复
热议问题