当前位置:网站首页>Test whether the time-consuming of vector, list and set calling empty and size is constant
Test whether the time-consuming of vector, list and set calling empty and size is constant
2022-07-19 12:05:00 【Apprentice Mo Xiaoge】
When reading code , Found use size()==0 Determine whether the container is empty , And from <<Effective STL>> See above size() Constant time cannot be guaranteed , It is recommended to use empty() Replace . So I did an experiment , Find out size() Constant time is not guaranteed , but empty Can guarantee .
/**
test vector、list、set call empty and size Is the time-consuming constant ,
Conclusion :empty() The call time of is constant ,list Of size() The call time of is non constant
Use advice : Use when judging whether the member is empty empty(), Instead of 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;
}边栏推荐
- 02-2、缺省参数、函数重载、引用、隐式类型转换、关于报错
- Research on downlink spectrum efficiency of 6G space earth integrated network high altitude platform base station
- C语言绘图示例-分色调图20例
- STL string输入输出重载
- 解决:Function rgb is missing argument $green. 的问题
- Lychee sound quality high fidelity AI noise reduction technology sharing
- Time consuming test of construction and sorting of set, vector and list
- TCP拥塞控制详解 | 7. 超越TCP
- Flink
- Dual machine hot standby of Huawei firewall (NGFW)
猜你喜欢

NAT technology and NAT alg

Stc8h development (XIV): I2C drive rx8025t high-precision real-time clock chip

windows10:vscode下go语言的适配

Total number of blocking and waiting in jconsole thread panel (RPM)
![[MySQL] add, delete, check and modify MySQL (Advanced)](/img/56/684204c509d3ce8db1397709216db7.png)
[MySQL] add, delete, check and modify MySQL (Advanced)

LeetCode_17_电话号码的字母组合

Kunlunbase online meetup is waiting for you~

Redis分布式緩存-Redis集群
![[binomial tree] the power of the button cattle customers must brush questions](/img/c0/254c28c5d94635e817fa6ba0b8483a.png)
[binomial tree] the power of the button cattle customers must brush questions

MGRE 环境下配置OSPF实验
随机推荐
渐变色按钮功能按钮绘制C语言示例
Flink
Region performance tuning
Dream CMS foreground search SQL injection
C语言绘图示例-繁花图案
任天堂专利显示健身环配件后续产品或已在开发中
解决:code ERESOLVE:ERESOLVE could not resolve 的报错问题
02 - 3. Différences entre les pointeurs et les références
FreeRTOS personal notes - protection of critical values
The adaptation of go language under windows10:vscode
STL string 输入输出重载1
HCIP(8)
TiKV 线程池性能调优
Dual machine hot standby of Huawei firewall (NGFW)
夢想CMS 前臺搜索SQL注入
Leetcode 1252. Number of odd value cells
Why does the magnetic variable speed gear reverse?
MGRE 环境下配置OSPF实验
Redis Distributed cache - Redis Cluster
TCP congestion control details | 7 Surpass TCP