什么更有效,i++ 还是++i?
精确重复:C++ 中 i++ 和 ++i 之间是否存在性能差异?
精确重复:循环中i++和++i之间的区别?
哪个更有效,i++或者++i?
我只在 Java 和 C/C++ 中使用过这个,但我真的要求所有实现这个的语言。
在大学里,我有一位教授向我们展示 ++i 效率更高,但已经过去了几年了,并且我想从 StackOverflow 社区获取意见。
Exact Duplicate: Is there a performance difference between i++ and ++i in C++?
Exact Duplicate: Difference between i++ and ++i in a loop?
What is more efficient, i++ or ++i?
I have only used this in Java and C/C++, but I am really asking for all languages that this is implemented in.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
i++ :
++i :
启用优化后,生成的程序集很可能是相同的,但 ++i 效率更高。
编辑:请记住,在 C++ 中,i 可以是支持前缀和后缀 ++ 运算符的任何对象。 对于复杂的对象,临时复制成本是不可忽略的。
i++ :
++i :
With optimizations on, it is quite possible that the resulting assembly is identical, however ++i is more efficient.
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 would look elsewhere for optimization potential.
效率不应该成为您关注的问题:它才是意义。 两者不相同,除非它们是独立的:一个操作值的预使用,另一个操作值的后置。
整数我;
我=1;
计算<< 我++; //返回1
int i;
我=1;
计算<< ++我; //Returns 2
当含义不重要时,大多数编译器会将 ++i 和 i++(例如在 for 循环中)翻译为相同的机器/VM 代码。
Efficiency shouldn't be your concern: it is meaning. The two are not the same, unless they are freestanding: one operates pre-use of the value, the other post.
int i;
i = 1;
cout << i++; //Returns 1
int i;
i = 1;
cout << ++i; //Returns 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.
这对于现代编译器来说并不重要。
相同
现代编译器会发现
v
未被使用,并且计算v
的代码是纯粹的(没有副作用)。 然后它将删除v
和赋值代码并生成此It does not matter on a modern compiler.
is the same as
A modern compiler will discover that
v
is unused and the code to calculatev
is pure (no side effects). Then it will removev
and the assignment code and will generate this这很重要! 特别是如果您使用自定义迭代器协议的 C++ 领域...
您应该使用前缀表示法来避免必要的复制开销,但它仅适用于迭代器,它不适用于内置本机类型,无论如何,这些都只是一条指令。
It does matter! Especially if you're in C++ land with custom iterator protocols...
You should use the prefix notation to avoid a necessary copying overhead but it only applies to iterators, it doesn't apply to builtin native types, those are just one instruction regardless.
++i 对于运算符++ 的重要实现来说可能更有效,但即使在这种情况下,编译器也可能能够优化掉中间临时数据。
++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 不需要临时变量来存储内容。像这样思考它们:
++i
i++
看到了吗? 后递增需要一个临时变量。 假设编译器没有为您解决所有问题,而它几乎肯定会这样做。
当然,更重要的是程序逻辑; 如果您太担心这个了...:)
++i doesn't need a temporary variable to store stuff in. Think of them like this:
++i
i++
See? Postincrement requires a temporary variable. Assuming the compiler doesn't sort it all out for you, which it almost certainly does.
Of course, more important is program logic; you run the risk of encountering The Sad Tragedy of Micro-Optimisation Theatre if you worry about this too much...:)
嗯,在 C++ 中,我相信它们有不同的用途,具体取决于您想要更新变量的时间。
效率不应该决定您何时使用其中一种而不是另一种,但我认为无论哪种方式它们都会具有相同的效率。
Well, in C++ I believe they have different uses, depending on when you want the variable updated.
Efficiency shouldn't determine when you use one over the other, but I would assume they would have the same efficiency either way.
除非我遗漏了什么,否则它们应该具有相同的效率。 它们都应该产生单个加法指令。 这只是添加指令发生的位置的问题:在代码行的开头或结尾。
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.
++i
更快,因为i++
必须存储i
,然后递增它,然后返回i
的存储值>。++i
只需递增i
然后返回它。++i
is faster becausei++
has to storei
, then increment it, then return the stored value ofi
.++i
simply incrementsi
then returns it.独立的“i++;” 或“++i;” 应该生成同样有效的代码。 如果您在表达式中使用它,就会出现差异,其中“副作用”就会发挥作用。
也就是说,曾经有一段时间,当“全世界都是 Vax”并且编译器很糟糕时,据说 ++i 比 i++ 更高效,即使在“for (i = 0; i < N; ++i)”类型设置。
A stand-alone "i++;" or "++i;" should generate equally efficient code. The difference comes if you're using it in an expression, where the "side effect" comes into play.
That said, there was a time, back when "all the world's a Vax", and compilers sucked, that ++i was said to be more efficient that i++, even in a "for (i = 0; i < N; ++i)" type setting.
一般来说,使用++i比i++效率更高。
完全相同
原因很简单,++i 与i += 1
; 对于 x86 来说,这是一条单指令(也可能是大多数其他广泛使用的体系结构)。
然而 i++ 等于
tmp = i; 我+= 1;
这是因为 i++ 的计算结果是“i”的旧值。 显然,这需要做更多的工作,而不仅仅是 i += 1;
但如上所述,这对于足够聪明的编译器几乎没有影响,因为它会优化未使用的操作。 对于许多解释性语言(例如:PHP),++i 的速度增益可能很小; 但这种增加可以忽略不计。
In general, it is more efficient to use ++i than i++.
The simple reason for this is that ++i is utterly the same as
i += 1;
which for x86 is a single instruction (and likely most other widely used architectures).
i++ is however equal to
tmp = i; i += 1;
That is because the old value of 'i' is what i++ evaluates to. And clearly that requires more work than simply i += 1;
But as stated above, this has virtually no impact with a sufficiently clever compiler, as it will optimize unused operations away. For many interpreted languages (example: PHP) there is likely a minimal gain in speed for the ++i; But this increase is negligible.
通常,输入 i++ 更容易,因此在生产时间方面更有效。
但说真的,如果i是本机数据类型(例如 int、double 等)——没有区别。
如果它是用户定义的类型,则取决于实现,例如
It's generally easier to type i++, hence it is more efficient in terms of productivity time.
Seriously, though, if i is a native data type (such as int, double, etc) -- no difference.
And it is implementation depended if it's a user-defined type such as
在未经优化的 x86 汇编中,++i 比 i++ 少一条处理器指令。
++i takes one less processor instruction than i++ in x86 assembly without optimization.
没有区别。 使用最有意义的结构。
如果您的应用程序运行缓慢,我可以向您保证,这永远不会是因为整数增量操作的速度差异。 如果是,则编译器中存在严重错误。 速度
应用程序中的问题将是算法效率低下、等待 I/O 等等。
不要担心你没有的问题。 过早优化是万恶之源。
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.
Don't worry about problems that you don't have. Premature optimization is the root of all evil.
这个堆栈溢出问题有一个很好的答案:是C 中的 i++ 和 ++i 之间有性能差异吗?
我想补充一点,您应该使用更适合您需求的一个。 除了在最关键的应用程序中,它并不重要。 从学术角度来看,最好编写能够表达您需要的代码并最终进行优化。
This Stack Overflow question has a great answer: Is there a performance difference between i++ and ++i in C?
I would like to add that you should use whichever one suits your needs better. Except in the most time critical of applications, it is not important. From an academic perspective too, it is better to write code that expresses what you need and optimize at last.
没有正确或错误的答案。
因为它取决于:
编译器如何实现它。
系统运行在什么CPU上。
如果
i
是字节或i
是双字There is no right or wrong answer.
As it depends on:
How it was implemented by the compiler.
What CPU the system runs on.
If
i
is byte ori
is double word这取决于上下文,例如:
在这种情况下,“x”将等于“i”,并且只有在那之后“i”才会加一。
在这种情况下,“i”将加一,然后“x”的新值将分配给“x”。
在“for”循环的情况下,除了性能之外几乎没有明显的差异(++i 更快)。
It depends upon the context, for example:
In this case, 'x' will be equal to 'i', and only after that will 'i' be increased by one.
In this case, 'i' will be increase by one and then the new value of 'x' will be assigned to 'x'.
In the case of a 'for' loop, there is little apparent difference other than performance (++i is faster).
一般来说,在C++中,postfix需要额外构造递增的对象,而prefix则直接应用于对象。 (或者我读过。)
由于我对此事的了解有限,我无法证明编译器如何处理它,因此可以为您处理它,使其成为一个没有实际意义的问题。
Generally, in C++, postfix will require the additional construction of the object incremented, while prefix is applied directly to the object. (Or so I've read.)
As I am unable to attest to how the compiler handles it due to my limited knowledge on the matter, it could be handled for you making it a moot point.
很难准确回答这个问题,因为它取决于编译器/解释器的实现。
但一般来说,您可以将 i++ 粗略地扩展为以下指令:
而 ++i 将粗略地扩展为:
您不能仅仅说 ++i 比 i++ 更快,因为语言实现非常智能,并且它们可以在以下情况下优化这些指令:您知道您不会访问 i++ 的临时值。 这通常发生在 for 循环中。 所以在很多情况下都是一样的。
如果您正在尝试进行此类微观优化,我建议您在选择其中之一之前先进行分析/测量。
It's hard to answer this precisely as it depends on the compiler/interpreter implementation.
But generally speaking you can say roughly extend i++ to the following instructions:
While ++i will roughly extend to:
You can't just say that ++i is faster than i++ since language implementations are pretty smart and they can optimize these instructions when you know that you won't access the temporary value of i++. This usually happens in say a for loop. So in many cases it's just the same.
If you're trying to these kind of micro-optimizations I'd advice you to profile/measure before chosing one over another.