STL 迭代器会优化后缀 ++/-- 运算符的低效问题吗?
我知道增量/减量运算符的后缀版本通常会由编译器针对内置类型进行优化(即不会进行复制),但迭代器是否属于这种情况?
它们本质上只是重载运算符,可以通过多种方式实现,但由于它们的行为是严格定义的,因此它们可以被优化吗?如果可以,它们是否由任何/许多编译器优化?
#include <vector>
void foo(std::vector<int>& v){
for (std::vector<int>::iterator i = v.begin();
i!=v.end();
i++){ //will this get optimised by the compiler?
*i += 20;
}
}
I know that the postfix versions of the increment/decrement operators will generally be optimised by the compiler for built-in types (i.e. no copy will be made), but is this the case for iterator
s?
They are essentially just overloaded operators, and could be implemented in any number of ways, but since their behaviour is strictly defined, can they be optimised, and if so, are they by any/many compilers?
#include <vector>
void foo(std::vector<int>& v){
for (std::vector<int>::iterator i = v.begin();
i!=v.end();
i++){ //will this get optimised by the compiler?
*i += 20;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 GNU GCC 的 STL 实现(版本 4.6.1)上的
std::vector
的特定情况下,我认为在足够高的优化级别上不会有性能差异。vector
上的前向迭代器的实现由__gnu_cxx::__normal_iterator
提供。让我们看看它的构造函数和后缀++
运算符:以及它在
vector
中的实例化:正如你所看到的,它内部对普通指针执行后缀增量,然后传递通过其自己的构造函数获取原始值,该构造函数将其保存到本地成员。通过死值分析可以轻松消除此代码。
但它真的优化了吗?让我们来看看吧。测试代码:
输出程序集(在
-Os
上):如您所见,两种情况下都会输出完全相同的程序集。
当然,对于自定义迭代器或更复杂的数据类型来说,情况可能不一定如此。但看起来,对于向量来说,前缀和后缀(不捕获后缀返回值)具有相同的性能。
In the specific case of
std::vector
on GNU GCC's STL implementation (version 4.6.1), I don't think there would be a performance difference on sufficiently high optimization levels.The implementation for forward iterators on
vector
is provided by__gnu_cxx::__normal_iterator<typename _Iterator, typename _Container>
. Let's look at its constructor and postfix++
operator:And its instantiation in
vector
:As you can see, it internally performs a postfix increment on an ordinary pointer, then passes the original value through its own constructor, which saves it to a local member. This code should be trivial to eliminate through dead value analysis.
But is it optimized really? Let's find out. Test code:
Output assembly (on
-Os
):As you can see, exactly the same assembly is output in both cases.
Of course, this may not necessarily be the case for custom iterators, or more complex data types. But it appears that, for
vector
specifically, prefix and postfix (without capturing the postfix return value) have identical performance.