#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. VSCode打开已有vuejs项目

    转载自 https://blog.csdn.net/yoryky/article/details/78290443 下载安装并配置VSCode 随便百度上搜个最新的VSCode安装好后,点击Ctrl ...

  2. django项目中关于跨域CORS

    1.使用django-cors-headers扩展,但首先进行安装 2.在配置中添加应用 3.在中间层中设置:“corsheaders.middleware.CorsMiddleware” 4.添加C ...

  3. Paper Reading - Mind’s Eye: A Recurrent Visual Representation for Image Caption Generation ( CVPR 2015 )

    Link of the Paper: https://ieeexplore.ieee.org/document/7298856/ A Correlative Paper: Learning a Rec ...

  4. python实现lower_bound和upper_bound

    由于对于二分法一直都不是很熟悉,这里就用C++中的lower_bound和upper_bound练练手.这里用python实现 lower_bound和upper_bound本质上用的就是二分法,lo ...

  5. ab命令做压测测试

    1. 背景:互联网发达的今天,大大小小的网站如雨后春笋,不断出现,但是想要做出一个网站很简单,但是想要做好一个网站,非常非常难,首先:网站做好之后的功能怎么样这都是次要的,主要的是你的网站能承受怎么样 ...

  6. 5.airflow问题

    1. Traceback (most recent call last): File "/usr/bin/airflow", line 28, in <module> ...

  7. 结对作业_Two

    Part 1.前言 (附:本次编码涵盖的所有功能均为java语言实现) 结对项目作业 结对同学高裕翔的博客 个人github传送门 博文pdf链接 Part 2.具体分工 本次的结对作业我们简单的拆分 ...

  8. View 渲染

    在Spring MVC 中,controllers不负责具体的页面渲染,仅仅是调用业务逻辑并返回model数据给view层,至于view层具体怎么展现,由专门的view层具体负责,这就是MVC模式,业 ...

  9. 【php】set_include_path和get_include_path用法详解

    目的:在框架中方便加载文件 参考:http://blog.sina.com.cn/s/blog_4ce89f200100twbl.html 如果我们没有设置这个值,可能我们需要写一些完全的路径:    ...

  10. 使用 Maven 管理项目

    最近的练手项目使用的是 Maven 在管理项目,在使用 Maven 管理项目时,三层的开发时分模块开发的,parent-dao-service-web,所有的spring+struts + Hiber ...