std::vector与std::list的执行速度比较 (C/C++) (STL)
来源:互联网 发布:富兰克林罗斯福知乎 编辑:程序博客网 时间:2024/05/20 19:32
STL中的container各有专长,最常用的是std::vector,可以完全取代array,第二常用的是std::list。std::vector的优点在于non-sequential access超快,新增数据于数据后端超快,但insert和erase任意资料则相当缓慢;std::list则是insert和erase速度超快,但non-sequential access超慢,此范例以实际时间比较vector和list间的优缺点。
1/*
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : VectorVsList.cpp
5Compiler : Visual C++ 8.0
6Description : Demo the performance difference between std::vector and std::list
7*/
8#include <iostream>
9#include <ctime>
10#include <vector>
11#include <list>
12
13// Add to Vector at end
14void addToVectorAtEnd();
15// Add to List at end
16void addToListAtEnd();
17// Calculate time for adding to end
18void addToEnd();
19
20// Add to Vector anywhere
21void addToVectorAnywhere();
22// Add to List anywhere
23void addToListAnywhere();
24// Calculate time for adding anywhere
25void addAnywhere();
26
27// Remove from Vector anywhere
28void removeFromVectorAnywhere();
29// Remove from List anywhere
30void removeFromListAnywhere();
31// Calculate time for removing anywhere
32void removeAnywhere();
33
34// Non-sequential access to Vector
35void nonSequentialAccessToVector();
36// Non-sequential access to List
37void nonSequentialAccessToList();
38// Caculate time for non-sequential access to
39void nonSequentialAccess();
40
41std::vector<int> vector1;
42std::list<int> list1;
43
44int main() {
45 // Calculate time for adding to end
46 addToEnd();
47
48 // Calculate time for adding anywhere
49 addAnywhere();
50
51 // Calculate time for removing anywhere
52 removeAnywhere();
53
54 // Caculate time for non-sequential access to
55 nonSequentialAccess();
56}
57
58// Add to Vector at end
59void addToVectorAtEnd() {
60 for(int i=0; i != 1000000; ++i) {
61 vector1.push_back(i);
62 }
63}
64
65// Add to List at end
66void addToListAtEnd() {
67 for(int i=0; i != 1000000; ++i) {
68 list1.push_back(i);
69 }
70}
71
72// Calculate time for adding to end
73void addToEnd() {
74 clock_t addToVectorAtEndClock = clock();
75 addToVectorAtEnd();
76 addToVectorAtEndClock = clock() - addToVectorAtEndClock;
77
78 std::cout << "Vector Insertion at the end Process time:" << (double)addToVectorAtEndClock/CLOCKS_PER_SEC << " sec" << std::endl;
79
80 clock_t addToListAtEndClock = clock();
81 addToListAtEnd();
82 addToListAtEndClock = clock() - addToListAtEndClock;
83
84 std::cout << "List Insertion at the end Process time:" << (double)addToListAtEndClock/CLOCKS_PER_SEC << " sec" << std::endl;
85
86 if (addToVectorAtEndClock < addToListAtEndClock) {
87 std::cout << "Insertion at the end : Vector wins" << std::endl;
88 }
89 else {
90 std::cout << "Insertion at the end : List wins" << std::endl;
91 }
92}
93
94// Add to Vector anywhere
95void addToVectorAnywhere() {
96 // Add to 50000th
97 std::vector<int>::iterator iter = vector1.begin();
98
99 for(int i = 0; i <= 500; ++i) {
100 ++iter;
101 iter = vector1.insert(iter,i);
102 }
103}
104
105// Add to List anywhere
106void addToListAnywhere() {
107 // Add to 50000th
108 std::list<int>::iterator iter = list1.begin();
109
110 for(int i = 0; i != 500; ++i) {
111 ++iter;
112 iter = list1.insert(iter,i);
113 }
114}
115
116// Calculate time for adding anywhere
117void addAnywhere() {
118 clock_t addToVectorAnywhereClock = clock();
119 addToVectorAnywhere();
120 addToVectorAnywhereClock = clock() - addToVectorAnywhereClock;
121
122 std::cout << "Vector Insertion anywhere Process time:" << (double)addToVectorAnywhereClock/CLOCKS_PER_SEC << " sec" << std::endl;
123
124 clock_t addToListAnywhereClock = clock();
125 addToListAnywhere();
126 addToListAnywhereClock = clock() - addToListAnywhereClock;
127
128 std::cout << "List Insertion anywhere Process time:" << (double)addToListAnywhereClock/CLOCKS_PER_SEC << " sec" << std::endl;
129
130
std::vector与std::list的执行速度比较 (C/C++) (STL)
std::vector与std::list的执行速度比较 (C/C++) (STL)
std::vector与std::list的执行速度比较 (C/C++) (STL)
std::vector与std::list的执行速度比较 (C/C++) (STL)
std::vector与std::list的执行速度比较 (C/C++) (STL)
std::vector与std::list的执行速度比较 (C/C++) (STL)
std vector与std list的效率比较
std vector与std list的效率比较
【语言-C++】STL std::list的使用
【c++】std vector的介绍
C++-std::Vector
C/C++字符串处理(4):std::vector与std::StringBuilder
C/C++字符串处理:std::vector与std::StringBuilder
std::vector,std::deque,std::list的区别的使用
std::set, std::list, std::vector在erase的区别
std::vector和std::list访问元素的速度真的很慢吗?
C++/STL std::string 的用法
[C/C++标准库]_[初级]_[std::deque和std::vector比较]
wince系统智能设备开发过程中出现找不到文件名
Cygwin安装openSSH
DICOM协议中有关打印的内容
即时通讯协议简述
linux上面的sz,rz命令与ssh的配合
std::vector与std::list的执行速度比较 (C/C++) (STL)
Linux 下获取某虚拟地址对应的页表信息
Linux(Fedora和Ubuntu等)下android真机调试
2010年的最后一周
std::vector与std::list的执行速度比较 (C/C++) (STL)
DVT和ADVT - 为数不多的DICOM测试工具说明AND下载
std::vector与std::list的执行速度比较 (C/C++) (STL)
Dcmtk 在 PACS 开发中的应用
床边数字x线机--概念机
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : VectorVsList.cpp
5Compiler : Visual C++ 8.0
6Description : Demo the performance difference between std::vector and std::list
7*/
8#include <iostream>
9#include <ctime>
10#include <vector>
11#include <list>
12
13// Add to Vector at end
14void addToVectorAtEnd();
15// Add to List at end
16void addToListAtEnd();
17// Calculate time for adding to end
18void addToEnd();
19
20// Add to Vector anywhere
21void addToVectorAnywhere();
22// Add to List anywhere
23void addToListAnywhere();
24// Calculate time for adding anywhere
25void addAnywhere();
26
27// Remove from Vector anywhere
28void removeFromVectorAnywhere();
29// Remove from List anywhere
30void removeFromListAnywhere();
31// Calculate time for removing anywhere
32void removeAnywhere();
33
34// Non-sequential access to Vector
35void nonSequentialAccessToVector();
36// Non-sequential access to List
37void nonSequentialAccessToList();
38// Caculate time for non-sequential access to
39void nonSequentialAccess();
40
41std::vector<int> vector1;
42std::list<int> list1;
43
44int main() {
45 // Calculate time for adding to end
46 addToEnd();
47
48 // Calculate time for adding anywhere
49 addAnywhere();
50
51 // Calculate time for removing anywhere
52 removeAnywhere();
53
54 // Caculate time for non-sequential access to
55 nonSequentialAccess();
56}
57
58// Add to Vector at end
59void addToVectorAtEnd() {
60 for(int i=0; i != 1000000; ++i) {
61 vector1.push_back(i);
62 }
63}
64
65// Add to List at end
66void addToListAtEnd() {
67 for(int i=0; i != 1000000; ++i) {
68 list1.push_back(i);
69 }
70}
71
72// Calculate time for adding to end
73void addToEnd() {
74 clock_t addToVectorAtEndClock = clock();
75 addToVectorAtEnd();
76 addToVectorAtEndClock = clock() - addToVectorAtEndClock;
77
78 std::cout << "Vector Insertion at the end Process time:" << (double)addToVectorAtEndClock/CLOCKS_PER_SEC << " sec" << std::endl;
79
80 clock_t addToListAtEndClock = clock();
81 addToListAtEnd();
82 addToListAtEndClock = clock() - addToListAtEndClock;
83
84 std::cout << "List Insertion at the end Process time:" << (double)addToListAtEndClock/CLOCKS_PER_SEC << " sec" << std::endl;
85
86 if (addToVectorAtEndClock < addToListAtEndClock) {
87 std::cout << "Insertion at the end : Vector wins" << std::endl;
88 }
89 else {
90 std::cout << "Insertion at the end : List wins" << std::endl;
91 }
92}
93
94// Add to Vector anywhere
95void addToVectorAnywhere() {
96 // Add to 50000th
97 std::vector<int>::iterator iter = vector1.begin();
98
99 for(int i = 0; i <= 500; ++i) {
100 ++iter;
101 iter = vector1.insert(iter,i);
102 }
103}
104
105// Add to List anywhere
106void addToListAnywhere() {
107 // Add to 50000th
108 std::list<int>::iterator iter = list1.begin();
109
110 for(int i = 0; i != 500; ++i) {
111 ++iter;
112 iter = list1.insert(iter,i);
113 }
114}
115
116// Calculate time for adding anywhere
117void addAnywhere() {
118 clock_t addToVectorAnywhereClock = clock();
119 addToVectorAnywhere();
120 addToVectorAnywhereClock = clock() - addToVectorAnywhereClock;
121
122 std::cout << "Vector Insertion anywhere Process time:" << (double)addToVectorAnywhereClock/CLOCKS_PER_SEC << " sec" << std::endl;
123
124 clock_t addToListAnywhereClock = clock();
125 addToListAnywhere();
126 addToListAnywhereClock = clock() - addToListAnywhereClock;
127
128 std::cout << "List Insertion anywhere Process time:" << (double)addToListAnywhereClock/CLOCKS_PER_SEC << " sec" << std::endl;
129
130
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
女暴风
暴风女
暴风激活
暴风城
暴风tv
暴风音影5
影视暴风
暴风音影
暴风影视
暴风图片
暴风语音
下载暴风影
暴风影院
暴风影
暴风影视库
暴风阴影
暴风vr
暴风转码
暴风魔镜4
暴风公司
暴风突击队
暴风魔镜3
暴风新娘泰国剧情介绍
暴风新娘泰国泰语中字
暴风新娘泰国
暴风新娘全集
暴风新娘全集免费观看
暴风私人影院
暴风新娘泰剧国语
泰剧暴风新娘
集合在暴风城
暴风集团被证监会立案调查
暴风被证监会调查
暴风集团股票
暴风新娘泰语中字
暴风科技股票
暴风城特产调料
300431暴风科技
泰剧暴风新娘国语版全集安徽
谁无暴风劲雨时
暴风新娘分集介绍