什么是更高效的i ++或++ i? [英] What is more efficient i++ or ++i?
问题描述
完全重复:在C ++中i ++和++ i之间的性能差异吗?
确切重复:循环中i ++和++ i之间的区别?
更有效的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更高效,但已经有几年了,我想从SO社区获得输入。
In college I had a professor show us that ++i was more efficient but it has been a couple years and I would like to get input from the SO community.
推荐答案
p> i ++:
- 创建i的临时副本
- 增加i
- 返回临时副本
++ i:
- 增加i
- 返回i
,这是非常可能的结果程序集是完全相同的,但++ i是更有效率。
With optimizations on, it is quite possible that the resulting assembly is identical, however ++i is more efficient.
编辑:记住在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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!