遍历vector容器的效率问题

 今天看到关于vector遍历效率问题,以前遍历的时候却没有关心这些,实为惭愧。自己写了点代码放在vs2012上运行,得到结果和原来的博客上内容不符合。看来应该还有与平台和编译器优化有关。

      代码如下:

  1. #include "stdafx.h"  
  2. #include <vector>  
  3. #include <algorithm>  
  4. #include <functional>  
  5. #include <iostream>  
  6.   
  7.   
  8.   
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.     class CTest  
  12.     {  
  13.     public:  
  14.         void add(){}  
  15.     };  
  16.   
  17.     typedef std::vector<CTest*> VEC;  
  18.     VEC::size_type nAmount = 1000000;  
  19.     std::vector<CTest*>  vec;  
  20.     vec.resize(nAmount);  
  21.     for (VEC::size_type i = 0; i < nAmount; ++i)  
  22.     {  
  23.         vec[i] = new CTest();  
  24.     }  
  25.   
  26.     DWORD dwStart, dwEnd;  
  27.   
  28.     // STL -- for_each  
  29.     dwStart = timeGetTime();  
  30.     std::for_each(vec.begin(), vec.end(), std::mem_fun<void, CTest>(&CTest::add));  
  31.     dwEnd   = timeGetTime();  
  32.     std::cout << "for_each:\t" << dwEnd - dwStart << std::endl;  
  33.   
  34.     // STL -- iterator  
  35.     dwStart = timeGetTime();  
  36.     std::vector<CTest*>::iterator itr_begin = vec.begin();  
  37.     std::vector<CTest*>::iterator itr_end = vec.end();  
  38.     for (; itr_begin != itr_end; ++itr_begin)  
  39.     {  
  40.         (*itr_begin)->add();  
  41.     }  
  42.     dwEnd   = timeGetTime();  
  43.     std::cout << "iterator:\t" << dwEnd - dwStart << std::endl;  
  44.   
  45.     // STL -- iterator2  
  46.     dwStart = timeGetTime();  
  47.     std::vector<CTest*>::iterator itr_begin2 = vec.begin();  
  48.     for (; itr_begin2 != vec.end(); ++itr_begin2)  
  49.     {  
  50.         (*itr_begin2)->add();  
  51.     }  
  52.     dwEnd   = timeGetTime();  
  53.     std::cout << "iterator:\t" << dwEnd - dwStart << std::endl;  
  54.   
  55.     // operator[]  
  56.     dwStart = timeGetTime();  
  57.     for (size_t i = 0; i < nAmount; ++i)  
  58.     {  
  59.         vec[i]->add();  
  60.     }  
  61.     dwEnd   = timeGetTime();  
  62.     std::cout << "operator:\t" << dwEnd - dwStart << std::endl;  
  63.   
  64.     system("pause");  
  65.     return 0;  
  66. }  

测试多次,结果相差不大,截图三张:


 结果表明,使用迭代器效果最差,使用STL算法和下标方式差不多。


1、for_each (vs2012版)

  1. template<class _InIt,  
  2.     class _Fn1> inline  
  3.     _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)  
  4.     {   // perform function for each element  
  5.     _DEBUG_RANGE(_First, _Last);  
  6.     _DEBUG_POINTER(_Func);  
  7.     _For_each(_Unchecked(_First), _Unchecked(_Last), _Func);  
  8.   
  9.   
  10.     return (_STD move(_Func));  
  11.     }  

2、end() (vs2012版)

  1. iterator end() _NOEXCEPT  
  2.         {   // return iterator for end of mutable sequence  
  3.         return (iterator(this->_Mylast, this));  
  4.         }  

3、operator[] (vs2012版)

  1. const_reference operator[](size_type _Pos) const  
  2.         {   // subscript nonmutable sequence  
  3.  #if _ITERATOR_DEBUG_LEVEL == 2  
  4.         if (size() <= _Pos)  
  5.             {   // report error  
  6.             _DEBUG_ERROR("vector subscript out of range");  
  7.             _SCL_SECURE_OUT_OF_RANGE;  
  8.             }  
  9.   
  10.  #elif _ITERATOR_DEBUG_LEVEL == 1  
  11.         _SCL_SECURE_VALIDATE_RANGE(_Pos < size());  
  12.  #endif /* _ITERATOR_DEBUG_LEVEL */  
  13.   
  14.         return (*(this->_Myfirst + _Pos));  
  15.         }  

特别的注意点是end(),每次调用end()就重新构建一个对象。第三种方法几乎是第二种的两倍。


===================================================

mem_fun和mem_fun_ref

前者处理容器中为指针类型,后者为类类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值