vector 的删除操作pop_back、erase效率对比 http://blog.csdn.net/efeics/article/details/8059690

来源:互联网 发布:手机h5页面制作软件 编辑:程序博客网 时间:2024/04/30 12:57
 

vector 的删除操作pop_back、erase效率对比

标签: vectorinclude
 4486人阅读 评论(0) 收藏 举报
 分类:
 
[cpp] view plain copy
  1. #include <vector>  
  2. #include <iostream>  
  3. #include <time.h>  
  4. using namespace std;  
  5.   
  6. struct Point   
  7. {  
  8.     int x;  
  9.     int y;    
  10.     Point():x(0),y(0)  
  11.     {}  
  12. };   
  13.   
  14. int main()  
  15. {  
  16.     clock_t s1,f1,s2,f2;  
  17.     s1=clock();  
  18.     vector<Point> point_vec(100000000);  
  19.     f1=clock();   
  20.     cout<<(double)(f1-s1)/CLOCKS_PER_SEC<<" S"<<endl;  
  21.       
  22.     s2=clock();  
  23. //      for (int i=0; i<10000000; ++i)  
  24. //      {  
  25. //          point_vec.pop_back();  
  26. //      }  
  27. //      point_vec.erase(point_vec.begin(),point_vec.begin()+10000000);  
  28.     point_vec.erase(point_vec.end()-10000000,point_vec.end());  
  29.     f2=clock();  
  30.     cout<<(double)(f2-s2)/CLOCKS_PER_SEC<<" S"<<endl;  
  31.       
  32.     return 0;  
  33. }  

VC++6.0

建立:

用strcut{int,int}类型来建立vector,10^8个,用时4.17s。

删除:

用erase删除vector最后的10^7个元素,用时0.25s。

用pop_back及循环删除最后的10^7个,用时1.02s。


用erase删除vector最前的10^7个元素,用时0.48s。

对于这一点比较惊讶,毕竟还要向前移动9*10^8个元素。


配置:Intel core i7处理器,8G内存。程序占用内存780M左右。


0 0
原创粉丝点击