在 C++ 中,预增量比后增量快 - 真的吗?如果是,为什么会这样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2020184/
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
Preincrement faster than postincrement in C++ - true? If yes, why is it?
提问by tvanfosson
Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
I heard about that preincrements (++i) are a bit faster than postincrements (i++) in C++. Is that true? And what is the reason for this?
我听说 C++ 中的前增量 (++i) 比后增量 (i++) 快一点。真的吗?这是什么原因?
回答by tvanfosson
Post-increment usually involves keeping a copy of the previous value around and adds a little extra code. Pre-increment simply does it's job and gets out of the way. I typically pre-increment unless the semantics would change and post-increment is actually necessary.
后增量通常涉及保留前一个值的副本并添加一些额外的代码。预增量只是完成它的工作并让开。除非语义会改变并且实际上需要后增量,否则我通常会预增量。
回答by Drew Hall
Postincrements have to make a copy of the object to return the unincremented value. For class types, this could be significant but for "int-like" types (incl. pointers), it's probably not.
后增量必须制作对象的副本以返回未增量的值。对于类类型,这可能很重要,但对于“int-like”类型(包括指针),可能不是。
回答by larsmoa
The difference for plain types types such as int is probably negligible. I would guess the compiler is able to optimize such cases (e.g. turn a postfix into a prefix increment where appropriate). However, for complex types (typically STL iterators the difference might be noticeable). The compiler is not able to switch to prefix in these cases because the operators actually might do totally different things. I recommend reading STL Iterators and Performancefor some background info on the performance penalty on using post-increment for STL iterators(short story: it takes twice the time as the pre-increment).
诸如 int 之类的普通类型类型的差异可能可以忽略不计。我猜编译器能够优化这种情况(例如,在适当的情况下将后缀转换为前缀增量)。但是,对于复杂类型(通常是 STL 迭代器),差异可能会很明显。在这些情况下,编译器无法切换到前缀,因为运算符实际上可能会做完全不同的事情。我建议阅读STL 迭代器和性能,以了解有关使用 STL 迭代器的后增量性能损失的背景信息(简短的故事:它需要两倍于前增量的时间)。
回答by Richard Pennington
On any decent compiler, ++i and i++ are identical if the value is not used.
在任何体面的编译器上,如果不使用该值,++i 和 i++ 是相同的。
If the value is used, ++i would need a temporary to store the pre-increment value if identical semantics were wanted.
如果使用该值,如果需要相同的语义,++i 将需要一个临时值来存储预增量值。
回答by Tobias Langner
actually - it depends. Postincrement needs a copy since it preserves the old value. If the type incremented is a complex type (e.g. an iterator) and not a simple type, the preincrement is faster than the postincrement. That is only true of course if you don't need the value before the increment.
实际上 - 这取决于。Postincrement 需要一个副本,因为它保留了旧值。如果增量的类型是复杂类型(例如迭代器)而不是简单类型,则前增量比后增量快。当然,如果您不需要增量之前的值,那才是正确的。
Some compilers might even detect that you don't need a postincrement and optimize the copy away - but as always - it's a bad habit to rely on that.
一些编译器甚至可能检测到您不需要后增量并优化副本 - 但一如既往 - 依赖它是一个坏习惯。