C++ 递增迭代器:++it 比 it++ 更有效吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1077026/
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
Incrementing iterators: Is ++it more efficient than it++?
提问by aem
Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
I am writing a program where an iterator is used to loop through a std::vector. Somebody told me that doing ++it in the for statement leads to more efficient code. In other words, they are saying that:
我正在编写一个程序,其中使用迭代器循环遍历 std::vector。有人告诉我在 for 语句中执行 ++it 会导致更高效的代码。换句话说,他们在说:
for ( vector<string>::iterator it=my_vector.begin(); it != my_vector.end(); ++it )
runs faster than
跑得比
for ( vector<string>::iterator it=my_vector.begin(); it != my_vector.end(); it++ )
Is this true? If it is, what is the reason behind the efficiency improvement? All it++/++it does is move the iterator to the next item in the vector, isn't it?
这是真的?如果是,效率提升背后的原因是什么?it++/++it 所做的就是将迭代器移动到向量中的下一项,不是吗?
回答by aem
The reason behind the preincrement being faster is that post-increment has to make a copy of the old value to return. As GotW #2put it, "Preincrement is more efficient than postincrement, because for postincrement the object must increment itself and then return a temporary containing its old value. Note that this is true even for builtins like int."
前增量更快的原因是后增量必须复制旧值才能返回。正如GotW #2所说,“前增量比后增量更有效,因为对于后增量,对象必须自增,然后返回一个包含其旧值的临时值。请注意,即使对于像 int 这样的内置函数也是如此。”
GotW #55provides the canonical form of postincrement, which shows that it has to do preincrement plus some more work:
GotW #55提供了postincrement的规范形式,这表明它必须做 preincrement 加上一些更多的工作:
T T::operator++(int)
{
T old( *this ); // remember our original value
++*this; // always implement postincrement
// in terms of preincrement
return old; // return our original value
}
As others have noted, it's possible for some compiler to optimize this away in some cases, but if you're not using the return value it's a good idea not to rely on this optimization. Also, the performance difference is likely to be very small for types which have trivial copy constructors, though I think using preincrement is a good habit in C++.
正如其他人所指出的,在某些情况下,某些编译器可能会对此进行优化,但如果您不使用返回值,最好不要依赖此优化。此外,对于具有简单复制构造函数的类型,性能差异可能非常小,尽管我认为在 C++ 中使用预增量是一个好习惯。
回答by Steve Jessop
It's unlikely to make any difference for a vector.
它不太可能对向量产生任何影响。
In general, ++it
is extremely unlikely to be slower than it++
(assuming a sensible implementation, if they're overloaded), and just might be faster. The reason is that if the iterator class itself is at all complex, then because it++
has to return the value before it
is incremented, the implementation will generally make a copy.
一般来说,++it
极不可能比it++
(假设一个合理的实现,如果它们过载)慢,并且可能会更快。原因是如果迭代器类本身很复杂,那么因为it++
必须在it
递增之前返回值,所以实现通常会进行复制。
Vector iterators are probably "just pointers" (in optimised, non-debug builds), and both operator++
s will be inlined. Since the return value is unused the copy will typically be elided. So it won't make any difference. I'm in the habit of typing ++it
because:
向量迭代器可能“只是指针”(在优化的非调试版本中),并且两个operator++
s 都将被内联。由于未使用返回值,因此副本通常会被省略。所以它不会有任何区别。我有打字的习惯,++it
因为:
1) Some day it might make a difference, for some iterator type, and I don't want to have to do something special for that type.
1) 有一天它可能会有所作为,对于某些迭代器类型,我不想为该类型做一些特别的事情。
2) Personally I think the prefix operator more clearly expresses the intent: "increment it", as opposed to "use it and then increment".
2)我个人认为前缀运算符更清楚地表达了意图:“增加它”,而不是“使用它然后增加”。
回答by thekidder
it++
performs the following operations:
it++
执行以下操作:
- create a copy of
it
- increment
it
- return the original (non-incremented)
it
- 创建一个副本
it
- 增量
it
- 返回原始(非递增)
it
++it
performs the following operations:
++it
执行以下操作:
- increment
it
- return
it
- 增量
it
- 返回
it
Because it++
creates a copy, it can be said to be "slower". However, any decent compiler will optimize this difference out for most defined types. For some user-defined types it canbe faster.
因为it++
创建副本,可以说是“慢”了。但是,任何体面的编译器都会针对大多数定义的类型优化这种差异。对于某些用户定义的类型,它可以更快。
回答by Thomas
Sometimes yes. With some it will be optimized away and be the same. For std::vector<> (and other std-iterators) it will most likely be optimized to be the same.
有时是的。有一些它将被优化掉并保持不变。对于 std::vector<> (和其他 std 迭代器),它很可能会被优化为相同的。
回答by Richard Berg
There is a chance that it++ will create a temporary copy.
it++ 有可能创建一个临时副本。
Also, in C++, there is a chance that someone has overloaded the postincrement operator.
此外,在 C++ 中,有可能有人重载了后增量运算符。
Both of these things could decrease performance vs preincrement. Neither is likely to matter in practice. The temporary copy, in particular, will be optimized away by most compilers since there are no side effects in the 3rd expression of your For loop.
与预增量相比,这两件事都可能降低性能。在实践中,两者都不太可能重要。特别是临时副本,大多数编译器都会优化掉,因为 For 循环的第三个表达式中没有副作用。
回答by oscarkuo
yes ++it is more efficient because it++ need to return a copy of the object then increment itself.
是的++它更有效,因为它++需要返回对象的副本然后自增。
回答by Stefano Borini
Yes. As far as I remember, ++it is more efficient than it++, because it++ creates a temporary object, while ++it does not.
是的。据我所知,++it 比 it++ 更有效,因为 it++ 创建了一个临时对象,而 ++it 没有。