已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
std:vector与std:list的执行速度比较 (C/C+) (STL) std:vector与std:list的执行速度比较 (C/C+) (STL) 收藏 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 3 4Filename : VectorVsList.cpp 5Compiler : Visual C+ 8.0 6Description : Demo the performance difference between std:vector and std:list 7*/ 8#include 9 #include 10#include 11 #include 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 vector1; 42std:list 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:iterator iter = vector1.begin(); 98 99 for(int i = 0; i = 500; +i) 100 +iter;101 iter = vector1.insert(iter,i);102 103104105/ Add to List anywhere106void addToListAnywhere() 107 / Add to 50000th108 std:list:iterator iter = list1.begin();109110 for(int i = 0; i != 500; +i) 111 +iter;112 iter = list1.insert(iter,i);113 114115116/ Calculate time for adding anywhere117void addAnywhere() 118 clock_t addToVectorAnywhereClock = clock();119 addToVectorAnywhere();120 addToVectorAnywhereClock = clock() - addToVectorAnywhereClock;121122 std:cout Vector Insertion anywhere Process time: (double)addToVectorAnywhereClock/CLOCKS_PER_SEC sec std:endl;123124 clock_t addToListAnywhereClock = clock();125 addToListAnywhere();126 addToListAnywhereClock = clock() - addToListAnywhereClock;127128 std:cout List Insertion anywhere Process time: (double)addToListAnywhereClock/CLOCKS_PER_SEC sec std:endl;129130 if (addToVectorAnywhereClock addToListAnywhereClock) 131 std:cout Insertion anywhere : Vector wins std:endl;132 133 else 134 std:cout Insertion anywhere : List wins std:endl;135 136137138/ Remove from Vector anywhere139 void removeFromVectorAnywhere() 140 std:vector:iterator iter = vector1.begin();141142 for(int i = 0; i != 500; +i) 143 +iter;144 iter = vector1.erase(iter);145 146147148/ Remove from List anywhere149void removeFromListAnywhere() 150 std:list:iterator iter = list1.begin();151152 for(int i = 0; i != 500; +i) 153 +iter;154 iter = list1.erase(iter);155 156157158/ Calculate time for removing anywhere159 void removeAnywhere() 160 clock_t removeFromVectorAnywhereClock = clock();161 removeFromVectorAnywhere();162 removeFromVectorAnywhereClock = clock() - removeFromVectorAnywhereClock;163164 std:cout Vector deletion anywhere Process time: (double)removeFromVectorAnywhereClock/CLOCKS_PER_SEC sec std:endl;165166 clock_t removeFromListAnywhereClock = clock();167 removeFromListAnywhere();168 removeFromListAnywhereClock = clock() - removeFromListAnywhereClock;169170 std:cout List deletion anywhere Process time: (double)removeFromListAnywhereClock/CLOCKS_PER_SEC sec std:endl;171172 if (removeFromVectorAnywhereClock removeFromListAnywhereClock) 173 std:cout Deletion anywhere : Vector wins std:endl;174 175 else 176 std:cout Deletion anywhere : List wins std:endl;177 178179180/ Non-sequential access to Vector181void nonSequentialAccessToVector() 182 for(int i = 0; i != 10000; +i) 183 int m = vector1.at(i);184 185186187/ Non-sequential access to List188void nonSequentialAccessToList() 189 for(int i = 0; i != 10000; +i) 190 std:list:const_iterator iter = list1.begin();191 for(int k = 0; k = i; +k) 192 +iter;193 194195 int m = (*iter);196 197198199/ Caculate time for non-sequential access to 200void nonSequentialAccess() 201 clock_t nonSequentialAccessToVectorClock = clock();202 nonSequentialAccessToVector();203 nonSequentialAccessToVectorClock = clock() - nonSequentialAccessToVectorClock;204205 std:cout Vector non-sequential access Process time: (double)nonSequentialAccessToVectorClock/CLOCKS_PER_SEC sec std:endl;206207 clock_t nonSequentialAccessToListClock = clock();208 nonSequentialAccessToList();209 nonSequentialAccessToListClock = clock() - nonSequentialAccessToListClock;210211 std:cout List non-sequential access Process time: (double)nonSequentialAccessToListClock/CLOCKS_PER_SEC sec std:endl;212213 if (nonSequentialAccessToVectorClock nonSequentialAccessToListClock) 214 std:cout Non-sequential : Vector wins std:endl;215 216 else 217 std:cout Non-sequential : List wins std:endl;218 219执行结果(Debug mode) 1Vector Insertion at the end Process time:3.094 sec 2List Insertion at the end Process time:10.205 sec 3Insertion at the end : Vector wins 4Vector Insertion anywhere Process time:5.548 sec 5List Insertion anywhere Process time:0.01 sec 6Insertion anywhere : List wins 7Vector deletion anywhere Process time:4.756 sec 8List deletion anywhere Process time:0.011 sec 9Deletion anywhere : List wins10Vector non-sequential access Process time:0.11 sec11List non-sequential access Process time:21.12 sec12Non-sequential : Vector wins(Release Mode) 1Vector Insertion at the end Process time:0.06 sec 2List Insertion at the end Process time:0.39 sec 3Insertion at the end : Vector wins 4Vector Insertion anywhere Process time:4.757 sec 5List Insertion anywhere Process time:0 sec 6Insertion anywhere : List wins 7Vector deletion anywhere Process time:4.717 sec 8List deletion anywhere Process time:0 sec 9Deletion anywhere : List wins10Vector non-sequential access Process time:0 sec11List non-sequential access Process time:0.491 sec12Non-sequential : Vector wins可见经过compiler优化后,速度差异非常明显本文来自CSDN博客,转载请标明出处:/ljx0305/archive/2009/09/05/4523408.aspxC+ error C2440: “类型转换” : 无法从“std:vector:iterator”转换为“PPkgHead”圆环套圆环之迭代器话说这一日是风平浪静,万里乌云,俺的心情好的没得说,收到命令清理A区(写部分代码,其中有在VC6下己完成的代码要移植到VC7下),一路上很轻松,用饭得标的话来说就是卡卡地!在快完成时出现错误。error C2440: “类型转换” : 无法从“std:vector:iterator”转换为“PPkgHead” with _Ty=BYTE 出错代码PPkgHead pHead = (PPkgHead )m_vPkgRecv.begin();晕之,在VC6下可以编译通过的呀!看看为啥不让转换呢?看了一下返回值是std:vector:iterator 或是 std:vector:const_iterator , 在begin()后面加了一个点看到了iterator有下面还有一个iterator(果然是圆环套圆环)和加、减、等运算符操作,很是郁闷应该怎么转换呢?到vector里看了一下原来是一个类 class iterator 在里面看到了几个函数 const_reference operator*() const / return designated object return
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 徐州市人民医院劳动合同订立变更续签实操考核
- 衢州市人民医院门脉高压急诊手术考核
- 扬州市人民医院靶向药物治疗监测与不良反应处理考核
- 池州市人民医院超声引导肿瘤消融考核
- 舟山市人民医院血液净化病房感控专员年度考核
- 绍兴市人民医院鼻腔泪囊吻合术技能考核
- 厦门市人民医院肺癌介入治疗考核
- 苏州市中医院高频电发生器原理与安全使用笔试试题
- 龙岩市中医院跨班组协作处理综合问题考核
- 储能设备维修与故障诊断方案
- GBZ/T(卫生) 201.5-2015放射治疗机房的辐射屏蔽规范第5部分:质子加速器放射治疗机房
- 非谓语动词在写作上的应用 课件 【知识导航+拓展迁移】高三英语一轮复习
- GB/T 1864-2012颜料和体质颜料通用试验方法颜料颜色的比较
- GB/T 13384-2008机电产品包装通用技术条件
- GA/T 167-2019法医学中毒尸体检验规范
- 国家储备林基地建设项目实施方案
- DB14-T 2498-2022检验检测机构人员技术档案管理指南-(高清最新)
- 症状性大脑中动脉慢性闭塞血管内开通治疗课件
- 胸腔积液健康教育
- 塔吊安装旁站监理记录表
- 设备清洁验证报告
评论
0/150
提交评论