Java i++ 或 ++i 哪个更有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/561588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is more efficient, i++ or ++i?
提问by Berek Bryan
Exact Duplicate: Is there a performance difference between i++ and ++i in C++?
Exact Duplicate: Difference between i++ and ++i in a loop?
完全重复:C++ 中的 i++ 和 ++i 之间是否存在性能差异?
完全重复:循环中 i++ 和 ++i 之间的区别?
What is more efficient, i++ or ++i?
i++ 或 ++i 哪个更有效?
I have only used this in Java and C/C++, but I am really asking for all languages that this is implemented in.
我只在 Java 和 C/C++ 中使用过它,但我真的要求所有实现它的语言。
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 的效率更高,但已经有几年了,我想从 Stack Overflow 社区获得意见。
采纳答案by Edouard A.
i++ :
我++:
- create a temporary copy of i
- increment i
- return the temporary copy
- 创建 i 的临时副本
- 增量 i
- 返回临时副本
++i :
++我:
- increment i
- return i
- 增量 i
- 返回我
With optimizations on, it is quite possible that the resulting assembly is identical, however ++i is more efficient.
通过优化,结果程序集很可能是相同的,但是 ++i 效率更高。
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.
编辑:请记住,在 C++ 中,我可能是支持前缀和后缀 ++ 运算符的任何对象。对于复杂对象,临时复制成本是不可忽略的。
回答by Ted
It does not matter on a modern compiler.
在现代编译器上无关紧要。
int v = i++;
is the same as
是相同的
int v = i;
i = i + 1;
A modern compiler will discover that v
is unused and the code to calculate v
is pure (no side effects). Then it will remove v
and the assignment code and will generate this
现代编译器会发现它v
是未使用的,并且要计算的代码v
是纯的(没有副作用)。然后它将删除v
和分配代码并生成这个
i = i + 1;
回答by kemiller2002
Well, in C++ I believe they have different uses, depending on when you want the variable updated.
好吧,在 C++ 中,我相信它们有不同的用途,具体取决于您希望何时更新变量。
Efficiency shouldn't determine when you use one over the other, but I would assume they would have the same efficiency either way.
效率不应该决定您何时使用一种而不是另一种,但我认为无论哪种方式它们都具有相同的效率。
回答by Steve Rowe
Unless I'm missing something, they should have the same efficiency. They should both result in a single add instruction. It's just a matter of where the add instruction takes place: at the beginning or the end of your line of code.
除非我遗漏了什么,否则它们应该具有相同的效率。它们都应该导致单个添加指令。这只是添加指令发生位置的问题:在代码行的开头或结尾。
回答by John Leidegren
It does matter! Especially if you're on C++...
这非常重要!特别是如果你使用 C++...
++i // the prefered way, unless..
auto j = i++ // this is what you need
You should always use the prefix notation to avoid necessary copying overhead and with C++ this matters!
您应该始终使用前缀符号来避免必要的复制开销,而对于 C++,这很重要!
回答by Tor Haugen
I would look elsewhere for optimization potential.
我会在别处寻找优化潜力。
回答by Joe Phillips
++i takes one less processor instruction than i++ in x86 assembly without optimization.
在没有优化的情况下,++i 在 x86 汇编中比 i++ 少一条处理器指令。
回答by Godeke
Efficiency shouldn't be your concern: it is meaning. The two are notthe same, unless they are freestanding: one operates pre-use of the value, the other post.
效率不应该是你关心的:它是意义。两者是不一样的,除非它们是独立的:一个操作值的预使用,另一个操作。
int i; i = 1; cout << i++; //Returns 1
国际我; 我 = 1; cout << i++; //返回1
int i; i = 1; cout << ++i; //Returns 2
国际我; 我 = 1; cout<<++i; //返回2
When meaning isn't important, most compilers will translate both ++i and i++ (say in a for loop) into the same machine/VM code.
当意义不重要时,大多数编译器会将 ++i 和 i++(例如在 for 循环中)翻译成相同的机器/VM 代码。
回答by jrockway
There is no difference. Use the construct that makes the most sense.
没有区别。使用最有意义的结构。
If your application runs slowly, I can guarantee you that it will never be because of speed differences in the integer increment operation. If it is, it's a severe bug in the compiler. Speed problems in your application will be algorithmic inefficiencies, waiting for I/O, and so on.
如果您的应用程序运行缓慢,我可以向您保证,它永远不会因为整数增量操作的速度差异。如果是,则是编译器中的严重错误。应用程序中的速度问题将是算法效率低下、等待 I/O 等。
Don't worry about problems that you don't have. Premature optimization is the root of all evil.
不要担心您没有的问题。过早优化是万恶之源。
回答by Jim Buck
++i is potentially more efficient for a non-trivial implementation of operator++, but even in that scenario, the compiler may be able to optimize away the intermediate temporary.
++i 对于 operator++ 的非平凡实现可能更有效,但即使在这种情况下,编译器也可以优化掉中间临时。