std :: list.begin()减慢了更大的列表

时间:2017-03-11 16:26:23

标签: performance list c++11 stl

我有以下问题。我需要实现必须使用双链表的LexBFS算法。我选择std::list。当我最终调试我的代码时,我注意到了奇怪的行为 - 随着列表的增长,列表操作变慢。我缩小到这一行:

// start new iteration
clock_t tbegin = clock();
if(L.size() && L.begin()->empty) {
  double ee = double(clock() - tbegin) / CLOCKS_PER_SEC;
  cerr << fixed << setprecision(6) << "t: " << ee << " size: " << L.size() << "\n";
  //...
}
// here I make other operations on L...

Lstd::list<Class>,其中Class是我的简单结构,包含几个字段。

这是输出:

t: 0.000086 size: 5224
t: 0.000124 size: 7818
t: 0.000281 size: 17515
t: 0.000300 size: 19310
t: 0.000341 size: 21202
t: 0.000406 size: 25117
t: 0.000459 size: 29202
t: 0.000510 size: 31307
t: 0.000528 size: 33413
t: 0.000562 size: 35487
t: 0.000638 size: 39740
t: 0.000706 size: 41885
t: 0.000710 size: 44037
t: 0.000747 size: 46261
t: 0.000868 size: 52670
t: 0.000982 size: 54800
t: 0.000957 size: 56895
t: 0.001084 size: 58993
t: 0.001114 size: 61101

简单的查找速度变慢了!发生了什么事?

提前致谢。

编译标志:

clang++ a.cpp -o ./a -std=c++11 -Wall -g 

Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)
Target: x86_64-pc-linux-gnu
Thread model: posix

1 个答案:

答案 0 :(得分:1)

http://www.cplusplus.com/reference/list/list/size/

在C ++ 98中,list.size()不需要是O(1)

尝试在C ++ 11模式下编译