vector的迭代器写操作 是不是要比下标慢很多?
c++吧
全部回复
仅看楼主
level 11
endrollex 楼主
测试代码:
#include <iostream>
#include <vector>
#include <deque>
#include <ctime>
using namespace std;
int main()
{
vector<char> vc1(1000000000, 1);
//test1
//for (vector<char>::size_type ix = 0; ix != vc1.size(); ++ix) vc1[ix] += 1;
//test2
for (vector<char>::iterator it = vc1.begin(); it != vc1.end(); ++it) *it += 1;
cout << "----" << endl;
int tc1 = clock();
cout << "spend time: " << (float)tc1/CLOCKS_PER_SEC << " seconds" << endl;
return 0;
}
用下标花费13秒左右,
而用迭代器花费53秒左右
测试有问题吗?

2012年10月30日 07点10分 1
level 15
release
2012年10月30日 07点10分 2
level 5
改成release版本以后使用迭代器只慢了0.1秒...
测试环境
CPU Core2 Q8300
RAM 8GB
Win7 X64
2012年10月30日 08点10分 3
忘了说..开发环境 VS2012
2012年10月30日 08点10分
level 11
endrollex 楼主
原来这样啊
我用编译器 cl -Ox 最大化优化
结果基本一样了[呵呵]
2012年10月30日 12点10分 4
1