当前位置:网站首页>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;
}边栏推荐
- function/symbol ‘pango_ context_ set_ round_ glyph_ positions‘ not found in library ‘libpango-1.0. so. 0‘x
- Choice is more important than effort
- Flink
- Nintendo patent shows that the follow-up products of fitness ring accessories may be under development
- rman异机恢复报错RMAN-06026 RMAN-06023
- Opencv draw a black rectangle and write the serial number
- Leetcode 1304. N different integers with zero and
- HCIP(4)
- LeetCode_ 216_ Combined sum III
- Enabling cities to "plan, build, operate, manage and serve" -- MAPGIS CIM platform explores "cim+" multi scenario applications
猜你喜欢
![[machine learning] evaluation index and code implementation of multi label classification](/img/01/7172841c663b5d9b79756e742bbc2c.png)
[machine learning] evaluation index and code implementation of multi label classification

Round table record: fireside dialogue -- how to realize innovation in Web3

Docker install MySQL

LeetCode_ 17_ Letter combination of telephone number

02-3. Difference between pointer and reference

Valid bracket sequence of "Niuke | daily question"

From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project

cv02-roge矩阵,旋转向量 ,角度

LeetCode_ 77_ combination

Energy saving resource allocation strategy based on time-varying graph in satellite networks
随机推荐
02 - 3. Différences entre les pointeurs et les références
[embedded unit test] construction of C language unit test framework
Leetcode 1328. Destroy palindrome string (yes, solved)
[unity technology accumulation] simple timer & Co process & delay function
function/symbol ‘pango_ context_ set_ round_ glyph_ positions‘ not found in library ‘libpango-1.0. so. 0‘x
From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project
解决:Function rgb is missing argument $green. 的问题
[original] magisk+shamiko has been tested by app root
Two misunderstandings of digital transformation
SQL union operator
Ten minutes from pytorch to mxnet
Choice is more important than effort
Round table record: fireside dialogue -- how to realize innovation in Web3
【二叉树】之力扣牛客必刷题
3.Golang字符串string类型
HCIP(5)
What happened to cinder in openstack-m
Time consuming test of construction and sorting of set, vector and list
cv02-roge矩阵,旋转向量 ,角度
数字化转型的两种误区