当前位置:网站首页>测试vector、list、set调用empty和size的耗时是否为常数
测试vector、list、set调用empty和size的耗时是否为常数
2022-07-17 11:22:00 【学徒漠筱歌】
在阅读代码时,发现有使用size()==0判断是否容器为空的,而从<<Effective STL>>上看到size()不能保证常数时间,建议使用empty()替换。因此我做了一个实验,发现size()并不能保证常数时间,但empty可以保证。
/**
测试vector、list、set调用empty和size的耗时是否为常数,
结论:empty()的调用时间都是常数,list的size()的调用时间非常数
使用建议:判断成员是否为空时使用empty(),而非size() == 0
input COUNT:100000(10W) 1000000(100W)
output
member count is:1000000
test vector.empty():
cost time(ms):0
test vector.size():
cost time(ms):0
---------------------
test list.empty():
cost time(ms):0
test list.size():
cost time(ms):8
---------------------
test set.empty():
cost time(ms):0
test set.size():
cost time(ms):0
---------------------
member count is:10000000
test vector.empty():
cost time(ms):0
test vector.size():
cost time(ms):0
---------------------
test list.empty():
cost time(ms):0
test list.size():
cost time(ms):79
---------------------
test set.empty():
cost time(ms):0
test set.size():
cost time(ms):0
---------------------
*/
#include <iostream>
#include <vector>
#include <set>
#include <list>
#include <sys/time.h>
using namespace std;
typedef unsigned long long ull;
#define COST_TIME_START \
{\
timeval start; \
gettimeofday(&start, NULL);
#define COST_TIME_END \
timeval end;\
gettimeofday(&end, NULL); \
cout << "cost time(ms):" << ((end.tv_sec*1000 + end.tv_usec/1000) - (start.tv_sec*1000 + start.tv_usec/1000)) << endl; \
}
int main (int argc, char **argv) {
cout << "member count is:" << COUNT << endl;
vector<ull> v;
v.assign(COUNT, 0);
cout << "test vector.empty():" << endl;
COST_TIME_START
v.empty();
COST_TIME_END
cout << "test vector.size():" << endl;
COST_TIME_START
v.size();
COST_TIME_END
cout << "---------------------" << endl;
list<ull> l;
l.assign(COUNT, 0);
cout << "test list.empty():" << endl;
COST_TIME_START;
l.empty();
COST_TIME_END;
cout << "test list.size():" << endl;
COST_TIME_START;
l.size();
COST_TIME_END;
cout << "---------------------" << endl;
set<ull> s;
for (ull i = 0; i < COUNT; ++i) {
s.insert(i);
}
cout << "test set.empty():" << endl;
COST_TIME_START
s.empty();
COST_TIME_END
cout << "test set.size():" << endl;
COST_TIME_START
s.size();
COST_TIME_END
cout << "---------------------" << endl;
return 0;
}边栏推荐
猜你喜欢

【摸鱼神器】UI库秒变低代码工具——表单篇(二)子控件

一汽丰田亚洲狮首次产品焕新

Part I - Fundamentals of C language_ 6. Function

齐岳供应负载亚甲基蓝的CuMOF纳米晶|原位生长在泡沫镍上FeMOF纳米片|氧化物纳米线/ZIF系MOFs糖葫芦状复合材料

Duilib implements tooltip custom mouse prompt window

闲谈工业企业全厂信息化规划

ES Restful操作

中国十大国民小吃,第一居然是它

Convert video format to GIF picture format

18. Shell Scripting (1)
随机推荐
npm使用
506.相对名次
第4章-一阶多智体系统一致性 -> 切换拓扑系统一致性【程序代码】
Mux256to1v,Hadd,Fadd
565. Array nesting / Sword finger offer II 001 Integer division
Convert video format to GIF picture format
Chapter VIII vector of STL
[C language] a brief introduction to the first C language program and data type
Chapter XI queue of STL
钴铁双金属有机骨架Cox/MIL-100(Fe)|光敏剂@[email protected]|负载CQDs的FeMIL101材料|mof试剂
sqli-labs(less-11)
华为昇思MindSpore详细教程
Build a server environment with node+express
es索引、类型(mapping)、文档、ik分词器
华为Ascend910运行yolov3教程
自己创建的模块 使用cmd打开报 ModuleNotFoundError: No module named 解决方案
Conversion between two-dimensional array and sparse array
Clwy authority management (II) -- user module
Traffic ranking 100W website
氨基的金属-有机骨架材料Fe-MOF,Fe-MIL-88NH2|Zr基金属-有机骨架催化剂(Pt-UiO-66)|齐岳生物