#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
char e[10000000];
array f;
int main(int argc, char *argv[])
{
decltype(clock()) beg;
int Index[] = {10, 1000, 100000, 10000000};
for (const auto &MaxIndex : Index)
{
vector a, as(MaxIndex);
list b1, b2;
forward_list c;
queue d;
string g;
deque h1, h2;
printf("MaxIndex: %d\n", MaxIndex);
beg = clock();
for (int i = 1; i <= MaxIndex; ++i)
a.push_back('a');
printf("vector insert: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 0; i < MaxIndex; ++i)
as[i] = 'a';
printf("vector(Array) insert: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 1; i <= MaxIndex; ++i)
g.push_back('a');
printf("String insert: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 1; i <= MaxIndex; ++i)
b1.push_back('a');
printf("list insert(back): %ld ms\n", clock() - beg);
beg = clock();
for (int i = 1; i <= MaxIndex; ++i)
b1.push_front('a');
printf("list insert(front): %ld ms\n", clock() - beg);
beg = clock();
for (int i = 1; i <= MaxIndex; ++i)
c.push_front('a');
printf("forward_list insert: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 1; i <= MaxIndex; ++i)
d.push('a');
printf("queue insert: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 1; i <= MaxIndex; ++i)
h1.push_back('a');
printf("deque insert(back): %ld ms\n", clock() - beg);
beg = clock();
for (int i = 1; i <= MaxIndex; ++i)
h2.push_back('a');
printf("deque insert(front): %ld ms\n", clock() - beg);
beg = clock();
for (int i = 0; i < MaxIndex; ++i)
e[i] = 'a';
printf("InterArray insert: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 0; i < MaxIndex; ++i)
f[i] = 'a';
printf("Array insert: %ld ms\n\n", clock() - beg);
if (MaxIndex == 10000000)
{
int temp{0};
beg = clock();
for (const auto &i : a)
temp = i;
printf("vector rangefor: %ld ms\n", clock() - beg);
beg = clock();
for (const auto &i : g)
temp = i;
printf("string rangefor: %ld ms\n", clock() - beg);
beg = clock();
for (const auto &i : b1)
temp = i;
printf("list rangefor: %ld ms\n", clock() - beg);
beg = clock();
for (const auto &i : c)
temp = i;
printf("forward_list rangefor: %ld ms\n", clock() - beg);
beg = clock();
for (const auto &i : h1)
temp = i;
printf("deque rangefor: %ld ms\n", clock() - beg);
beg = clock();
for (const auto &i : e)
temp = i;
printf("InterArray rangefor: %ld ms\n", clock() - beg);
beg = clock();
for (const auto &i : f)
temp = i;
printf("Array rangefor: %ld ms\n\n", clock() - beg);
beg = clock();
for (int i = 0; i < MaxIndex; i += 50)
temp = a[i];
printf("vector random: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 0; i < MaxIndex; i += 50)
temp = g[i];
printf("string random: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 0; i < MaxIndex; i += 50)
temp = h1[i];
printf("deque random: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 0; i < MaxIndex; i += 50)
temp = e[i];
printf("InterArray random: %ld ms\n", clock() - beg);
beg = clock();
for (int i = 0; i < MaxIndex; i += 50)
temp = f[i];
printf("Array random: %ld ms\n\n", clock() - beg);
}
}
return EXIT_SUCCESS;
}
学习C++Primer顺序容器时,想自己简单测试下各个容器的插入和遍历速度。
MaxIndex: 10
vector insert: 7 ms
vector(Array) insert: 0 ms
String insert: 1 ms
list insert(back): 3 ms
list insert(front): 2 ms
forward_list insert: 1 ms
queue insert: 0 ms
deque insert(back): 0 ms
deque insert(front): 1 ms
InterArray insert: 0 ms
Array insert: 5 ms
MaxIndex: 1000
vector insert: 45 ms
vector(Array) insert: 7 ms
String insert: 15 ms
list insert(back): 148 ms
list insert(front): 155 ms
forward_list insert: 130 ms
queue insert: 39 ms
deque insert(back): 35 ms
deque insert(front): 35 ms
InterArray insert: 4 ms
Array insert: 8 ms
MaxIndex: 100000
vector insert: 3948 ms
vector(Array) insert: 713 ms
String insert: 757 ms
list insert(back): 14673 ms
list insert(front): 15040 ms
forward_list insert: 12743 ms
queue insert: 3892 ms
deque insert(back): 3518 ms
deque insert(front): 3391 ms
InterArray insert: 420 ms
Array insert: 776 ms
MaxIndex: 10000000
vector insert: 390553 ms
vector(Array) insert: 71997 ms
String insert: 81946 ms
list insert(back): 1441278 ms
list insert(front): 1470762 ms
forward_list insert: 1233765 ms
queue insert: 397169 ms
deque insert(back): 352025 ms
deque insert(front): 342730 ms
InterArray insert: 41664 ms
Array insert: 77833 ms
vector rangefor: 216657 ms
string rangefor: 222824 ms
list rangefor: 524961 ms
forward_list rangefor: 259790 ms
deque rangefor: 214309 ms
InterArray rangefor: 55341 ms
Array rangefor: 55207 ms
vector random: 2795 ms
string random: 2241 ms
deque random: 22443 ms
InterArray random: 2624 ms
Array random: 3788 ms
电脑比较旧 AMD速龙 ii x3 445;DDR3 1333 4GBx2;Ubuntu 18.04;g++ -std11
1.内置数组
2.确定大小后的vector,array,string(内置数组的2倍左右)
3.deque,vector(内置数组的8~9倍左右)
4.forward_list 1.array,内置数组 2.deque,vector,string(内置数组的4倍左右) 3.forward_list(内置数组的5倍左右) 4.list(内置数组的10倍左右) 1.内置数组 2.string 3.vector 4.array 5.deque(比上面的慢不少!) 追求算法速度 —— 内置数组; 安全&功能丰富的数组…2333 —— array; 需要基本的容器以完成实时扩张、收缩、随机访问 —— vector; 需要队列的算法(前后插入/删除) —— deque; 需要方便的字符串函数,更方便的处理字符串 —— string; 迫不得已必须要在任何位置大量的插入、删除 —— list、forward_list;(追求性能——forward_list / 你自己写吧…) 简单且常用的数据结构 —— queue(单向链表)、stack(堆栈)、priority_queue(优先队列) 需要顺序容器,内置数组太Low,不知道该用什么 —— vector;
遍历速度
随机访问速度
选择
END