我有以下代码:
main() {
vector<int> v (1000,0);
list<int> l (1000,0);
clock_t begin,end;
cout <<"Vector size: "<<v.size()<<endl;
cout <<"List size: "<<l.size()<<endl;
begin=clock();
for (int i=0;i<1000000;i++)
for (vector <int>::iterator it=v.begin();it!=v.end();it++);
end=clock();
cout <<v[0]<<endl;
cout << "Vector iteration: " << (double)(end-begin)/CLOCKS_PER_SEC <<endl;
begin=clock();
for (int i=0;i<1000000;i++)
for (list <int>::iterator it=l.begin();it!=l.end();it++);
end=clock();
list <int>::iterator it=l.begin();
cout << *it <<endl;
cout << "List iteration: " << (double)(end-begin)/CLOCKS_PER_SEC <<endl;
}
我总是得到向量迭代(大约)18.3 秒和列表迭代(大约)11.7 秒的结果。这怎么可能?我的测量有问题吗?
谢谢你的帮助!