#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中对容器的各种循环遍历的效率比较的更多相关文章

  1. 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 ...

  2. javascript中常见的几种循环遍历

    项目开发中,不管是建立在哪个框架基础上,对数据的处理都是必须的,而处理数据离不开各种遍历循环.javascript中循环遍历有很多种方式,记录下几种常见的js循环遍历. 一.for循环 for循环应该 ...

  3. Java中迭代列表中数据时几种循环写法的效率比较

    Java中经常会用到迭代列表数据的情况,本文针对几种常用的写法进行效率比较.虽然网上已经有了类似的文章,但是对他们的结论并不认同. 常见的实现方法: 1.for循环: for(int i = 0; i ...

  4. js替换数组中的一个对象用for循环遍历

    for(let i=0;i<statusList.length;i++){ if (statusList[i]['tableId'] === tableId) { statusList[i]=d ...

  5. JSP中使用<c:forEach>标签循环遍历元素

    转载:http://blog.csdn.net/hero_cheng/article/details/51924577

  6. 一起学习c++11——c++11中的新增的容器

    c++11新增的容器1:array array最早是在boost中出现:http://www.boost.org/doc/libs/1_61_0/doc/html/array.html 当时的初衷是希 ...

  7. java中Collection容器

    1.容器(Collection)也称为集合, 在java中就是指对象的集合. 容器里存放的都只能是对象. 实际上是存放对象的指针(头部地址): 这里对于八种基本数据类型,在集合中实际存的是对应的包装类 ...

  8. 论C++11 中vector的N种遍历方法

    随着C++11标准的出现,C++标准添加了许多有用的特性,C++代码的写法也有比较多的变化. vector是经常要使用到的std组件,对于vector的遍历,本文罗列了若干种写法. (注:本文中代码为 ...

  9. STL中的容器

    STL中的容器 一. 种类: 标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器slist ...

随机推荐

  1. JavaScript/Jquery:Validform 验证表单的相关属性解释

    当我们写提交表单的时候往往需要验证表单是否填写了内容,是否正确,这个插件可以很方便的完成我们需要的验证! 使用方法: 1.先引用js <script type="text/javasc ...

  2. 由A到D中间可不止“B、C”

    在电子信息系统的学习中,我们或许早就被告知现实世界是模拟的,而数字化的模拟世界则越来越展现更多的风采.但是所谓的数字和模拟只是相对的而已,你可以把模拟量当做无穷数字量的组合,也可以把数字量当做具有不同 ...

  3. Scrum立会报告+燃尽图(十一月十七日总第二十五次):设计调查问卷;修复上一阶段bug

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  4. git中的重要指令

    git命令 任何操作都需要以 git 命令为开头 本地操作: git init 初始化一个本地仓库 新建为 master主分支 git status 查看当前分支状态 git add <文件名& ...

  5. MathExamV2.0四则混合运算计算题生成器

    MathExamV2.0四则混合运算计算题生成器----211606360 丁培晖 211606343 杨宇潇 一.预估与实际 PSP2.1 Personal Software Process Sta ...

  6. 0324操作系统cmd功能的扩展

    需求:1.实现清屏功能 2.实现不区分大小写功能 3.添加功能能添加新的命令符 设计:1.使用system("cls")清屏. 2.使用strlwr()函数把大写都变成小写 3.( ...

  7. web移动端

    h5:低版本(IE8及以下不支持H5标签,要引入html5shiv.js才能正常运行) 条件引入,只是针对PC端,移动端不存在这样的操作 <figure>:专门用来存放图片和相关介绍的 & ...

  8. HDU 2113 Secret Number

    http://acm.hdu.edu.cn/showproblem.php?pid=2113 Problem Description 有一天, KIKI 收到一张奇怪的信, 信上要KIKI 计算出给定 ...

  9. 微信小程序组件 360

    data: { nums: 1, start: '', // change:'' // 上一部记忆数据 mid: '' }, mytouchmove: function (e) { var start ...

  10. 第112天:javascript中函数预解析和执行阶段

    关于javascript中的函数:  1.预解析:把所有的函数定义提前,所有的变量声明提前,变量的赋值不提前  2.执行 :从上到下执行,但有例外(setTimeout,setInterval,aja ...