C++11中对容器的各种循环遍历的效率比较
#include "CycleTimeTst.h"
#include <string>
#include <vector>
#include <list>
#include <limits>
#include <assert.h>
#include <QTime>
#include <QDebug> class TimeUsedGuard
{
public:
TimeUsedGuard(const std::string& msg)
: m_msg(msg)
, m_time(QTime::currentTime())
{
}
~TimeUsedGuard()
{
qDebug() << m_msg.c_str() << "use time:" << m_time.elapsed() << "ms";
} private:
std::string m_msg;
QTime m_time;
}; template <typename T>
T useData(T val)
{
return std::sqrt(val);
} template <typename _Container>
void normalForLoop(const _Container& datas)
{
TimeUsedGuard timeUsedGuard(__FUNCTION__);
size_t i = ;
for (; i < datas.size(); ++i)
{
useData(datas[i]);
}
} template <typename _Container>
void normalForLoopCallSizeOnce(const _Container& datas)
{
TimeUsedGuard timeUsedGuard(__FUNCTION__);
size_t i = ;
size_t size = datas.size();
for (; i < size; ++i)
{
useData(datas[i]);
}
} template <typename _Container>
void iterator(const _Container& datas)
{
TimeUsedGuard timeUsedGuard(__FUNCTION__);
auto pos = datas.cbegin();
for (; pos != datas.cend(); ++pos)
{
useData(*pos);
}
} template <typename _Container>
void iteratorCallCEndOnce(const _Container& datas)
{
TimeUsedGuard timeUsedGuard(__FUNCTION__);
auto pos = datas.cbegin();
auto end = datas.cend();
for (; pos != end; ++pos)
{
useData(*pos);
}
} template <typename _Container>
void qtForeach(const _Container& datas)
{
TimeUsedGuard timeUsedGuard(__FUNCTION__);
foreach (auto data, datas)
{
useData(data);
}
} template <typename _Container>
void stdForeach(const _Container& datas)
{
TimeUsedGuard timeUsedGuard(__FUNCTION__);
std::for_each(datas.cbegin(), datas.cend(), useData<typename _Container::value_type>);
} template <typename _Container>
void rangeForLoop(const _Container& datas)
{
TimeUsedGuard timeUsedGuard(__FUNCTION__);
for (auto data : datas)
{
useData(data);
}
} template <typename _Container>
void rangeForLoopReference(const _Container& datas)
{
TimeUsedGuard timeUsedGuard(__FUNCTION__);
for (auto& data : datas)
{
useData(data);
}
} class CycleTimeTst
{
public:
CycleTimeTst();
}; CycleTimeTst g_CycleTimeTst;
CycleTimeTst::CycleTimeTst()
{
constexpr unsigned long MAX_AMOUNT = * * ; // std::vector<int> datas;
// for (unsigned long i = 1; i < MAX_AMOUNT; ++i)
// {
// datas.push_back(i);
// }
std::list<double> datas;
double d = 1.0;
for (unsigned long i = ; i < MAX_AMOUNT / ; ++i)
{
datas.push_back(d);
} // normalForLoop(datas);
// normalForLoopCallSizeOnce(datas);
iterator(datas);
iteratorCallCEndOnce(datas);
qtForeach(datas);
stdForeach(datas);
rangeForLoop(datas);
rangeForLoopReference(datas);
// vector<int> 100 * 1000 * 1000 times, debug & release:
// normalForLoop use time: 1096 ms normalForLoop use time: 113 ms
// normalForLoopCallSizeOnce use time: 926 ms normalForLoopCallSizeOnce use time: 106 ms
// iterator use time: 1941 ms iterator use time: 103 ms
// iteratorCallCEndOnce use time: 1473 ms iteratorCallCEndOnce use time: 101 ms
// qtForeach use time: 1846 ms qtForeach use time: 262 ms
// stdForeach use time: 1488 ms stdForeach use time: 120 ms
// rangeForLoop use time: 1527 ms rangeForLoop use time: 101 ms
// rangeForLoopReference use time: 1551 ms rangeForLoopReference use time: 102 ms // list<double> 10 * 1000 * 1000 times, debug & release:
// iterator use time: 206 ms iterator use time: 27 ms
// iteratorCallCEndOnce use time: 183 ms iteratorCallCEndOnce use time: 26 ms
// qtForeach use time: 1493 ms qtForeach use time: 743 ms
// stdForeach use time: 182 ms stdForeach use time: 27 ms
// rangeForLoop use time: 186 ms rangeForLoop use time: 26 ms
// rangeForLoopReference use time: 186 ms rangeForLoopReference use time: 27 ms
}
个人总结:
vector支持随机访问,但无通用性,当采用list后,就不能再使用了,不可取.
迭代器与范围for循环所用时间无明显差异, qt提供的foreach显然是最差的方法, std算法for_each也还可以接受, 但范围for循环好看得多, 更好用啊!
在循环中调用调用cend方法,会频繁创建一个新的迭代器(不停的构造与析构),另外还有函数调用开销,在debug版本结果来看似乎确实有一定的影响,但在realease版本下所花时间几乎相等,也许编译器有优化吧!
C++11中对容器的各种循环遍历的效率比较的更多相关文章
- JavaScript 中的常用12种循环遍历(数组或对象)的方法
1.for 循环 let arr = [1,2,3]; for (let i=0; i<arr.length; i++){ console.log(i,arr[i]) } // 0 1 // 1 ...
- javascript中常见的几种循环遍历
项目开发中,不管是建立在哪个框架基础上,对数据的处理都是必须的,而处理数据离不开各种遍历循环.javascript中循环遍历有很多种方式,记录下几种常见的js循环遍历. 一.for循环 for循环应该 ...
- Java中迭代列表中数据时几种循环写法的效率比较
Java中经常会用到迭代列表数据的情况,本文针对几种常用的写法进行效率比较.虽然网上已经有了类似的文章,但是对他们的结论并不认同. 常见的实现方法: 1.for循环: for(int i = 0; i ...
- js替换数组中的一个对象用for循环遍历
for(let i=0;i<statusList.length;i++){ if (statusList[i]['tableId'] === tableId) { statusList[i]=d ...
- JSP中使用<c:forEach>标签循环遍历元素
转载:http://blog.csdn.net/hero_cheng/article/details/51924577
- 一起学习c++11——c++11中的新增的容器
c++11新增的容器1:array array最早是在boost中出现:http://www.boost.org/doc/libs/1_61_0/doc/html/array.html 当时的初衷是希 ...
- java中Collection容器
1.容器(Collection)也称为集合, 在java中就是指对象的集合. 容器里存放的都只能是对象. 实际上是存放对象的指针(头部地址): 这里对于八种基本数据类型,在集合中实际存的是对应的包装类 ...
- 论C++11 中vector的N种遍历方法
随着C++11标准的出现,C++标准添加了许多有用的特性,C++代码的写法也有比较多的变化. vector是经常要使用到的std组件,对于vector的遍历,本文罗列了若干种写法. (注:本文中代码为 ...
- STL中的容器
STL中的容器 一. 种类: 标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器slist ...
随机推荐
- 性能度量RMSE
回归问题的典型性能度量是均方根误差(RMSE:Root Mean Square Error).如下公式. m为是你计算RMSE的数据集中instance的数量. x(i)是第i个实例的特征值向量 ,y ...
- 二分图最大匹配模版 m√(n) 复杂度
周大爷在比赛中搜到的黑科技二分图模版,复杂度为m√(n): 注意:点的序号要从0开始! 需要把nx,ny都赋值为n(点数) ; *; struct Edge { int v; int next; } ...
- eBay:美国各州最受欢迎的产品品类
雨果网从美国媒体<商业内幕>8月26日的报道中获悉,电商巨头eBay近日发布了美国各州最受欢迎的产品品类.包括:加州人青睐女性高端配件,而新泽西 州的男人喜欢古龙香水.相比这些华丽配饰而言 ...
- ASP.NET Web API - 使用 Castle Windsor 依赖注入
示例代码 项目启动时,创建依赖注入容器 定义一静态容器 IWindsorContainer private static IWindsorContainer _container; 在 Applica ...
- TCP的三次握手(建立连接)和四次挥手(关闭连接)(转)
转自:(http://www.cnblogs.com/Jessy/p/3535612.html) 参照: http://course.ccniit.com/CSTD/Linux/reference/f ...
- 为phpStorm 配置PHP_CodeSniffer自动检查代码
通过composer 安装PHP_CodeSniffer : squizlabs/PHP_CodeSniffer gihub地址 composer global require "squiz ...
- SQLSERVER 根据身份证号码 往出生年月日 赋值
update CREW_SailorInfo set DT_DOB= ( case then , ) then , ) else null end) 注:此问题仅供参考 如有疑问 请加QQ群18153 ...
- 5月5号周二课堂练习:简评cnblogs.com的用户体验
一.用户类型 在博客园上写博客,提问题,浏览感兴趣的博客帖子的活跃用户. 二.对cnblogs的期望 在博客园上写博客更流畅,制作手机版的APP可以随时随地在线浏览大牛们写的博客,提出的问题能更好的更 ...
- 2018软工实践—Alpha冲刺(2)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助前端界面的开发 搭建测试用服务器的环境 完成 ...
- 团队作业4——第一次项目冲刺(Alpha版本)第一次
一.会议内容 制定任务内容 制作leangoo表格 初步工作 二.各人工作 成员 计划任务 遇见难题 贡献比 塗家瑜(组长) 后端与数据库通讯 无 1 张新磊 表设计 无 1 姚燕彬 测试计划编写 无 ...