问题描述
完全重复: i ++和++ i在C ++中是否存在性能差异?
完全重复: i ++和++ i循环之间的区别?
Exact Duplicate: Is there a performance difference between i++ and ++i in C++?
Exact Duplicate: Difference between i++ and ++i in a loop?
使用i ++或++ i效率更高?
What is more efficient, i++ or ++i?
我只在Java和C/C ++中使用过此语言,但我实际上是要求使用此语言实现的所有语言.
I have only used this in Java and C/C++, but I am really asking for all languages that this is implemented in.
在大学里,我有一位教授向我们展示了++ i效率更高,但这已经有几年了,我想从Stack Overflow社区中获取意见.
In college I had a professor show us that ++i was more efficient, but it has been a couple of years, and I would like to get input from the Stack Overflow community.
推荐答案
i ++:
- 创建i的临时副本
- 增加我
- 返回临时副本
++ i:
- 增加我
- 返回我
启用优化后,最终的程序集很可能是相同的,但是++ i效率更高.
With optimizations on, it is quite possible that the resulting assembly is identical, however ++i is more efficient.
edit:请记住,在C ++中,我可能是支持前缀和后缀++运算符的任何对象.对于复杂的对象,临时复制成本不可忽略.
edit : keep in mind that in C++, i may be whatever object that support the prefix and postfix ++ operator. For complex objects, the temporary copy cost is non negligible.
这篇关于什么是i ++或++ i效率更高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!