小编Pav*_*daj的帖子

为什么std :: list.size()不是常量时间?

此代码运行0.012秒:

 std::list<int> list;
 list.resize(100);
 int size;
 for(int i = 0 ; i < 10000; i++)
     size = list.size();
Run Code Online (Sandbox Code Playgroud)

这个为9.378秒:

 std::list<int> list;
 list.resize(100000);
 int size;
 for(int i = 0 ; i < 10000; i++)
     size = list.size();
Run Code Online (Sandbox Code Playgroud)

在我看来,有可能以这种方式实现std :: list,该大小将存储在私有变量中,但根据这个,每次调用size时都会再次计算.有谁能解释为什么?

c++ size performance list

17
推荐指数
2
解决办法
1657
查看次数

标签 统计

c++ ×1

list ×1

performance ×1

size ×1