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 ...
随机推荐
- win2003系统网络安装——基于linux+pxe+dhcp+tftp+samba+ris
原文发表于:2010-09-16 转载至cu于:2012-07-21 一.原理简介 PXE(preboot execute environment)工作于Client/Server的网络模式,支持工作 ...
- Linux内核学习笔记(6)-- 进程优先级详解(prio、static_prio、normal_prio、rt_priority)
Linux 中采用了两种不同的优先级范围,一种是 nice 值,一种是实时优先级.在上一篇粗略的说了一下 nice 值和实时优先级,仍有不少疑问,本文来详细说明一下进程优先级.linux 内核版本为 ...
- resx文件引用
应用场景: 自己在编写双语界面的时候,用到两种语言表. 引用如下: LocRM = new ResourceManager("TMI_E.words_" + lang.ToLowe ...
- FPGA论文
基于 NetFPGA 的 VCP 网络的设计与实现 --可变结构拥塞控制协议(VCP),适应于高带宽时延乘积网络的显式拥塞控制协议 无源光网络(PON) 1.区块链技术发展,物联网设备激增,服务器压力 ...
- 技术博客HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- lintcode-507-摆动排序 II
507-摆动排序 II 给你一个数组nums,将它重排列如下形式 nums[0] < nums[1] > nums[2] < nums[3].... 注意事项 你可以认为每个输入都有 ...
- Java多线程下单例
/* 多线程下的单例 */ //饿汉式 class Single { private static final Single s = new Single(); private Single(){} ...
- ranch代码简述
最近要看一下erlang连接池,觉得ranch很不错. github上面有人写了ranch的代码阅读,可以看一下,链接在这里. 1. ranch可以同时监听多个端口,每个端口的连接信息可以单独配置. ...
- jdbc 5.0
1.事务 事务将单个SQL语句或一组SQL语句视为一个逻辑单元,如果任何语句失败,整个事务将失败. jdbc的MySQL驱动程序中的事务默认是自动提交. 默认情况下,每个SQL语句在完成后都会提交到数 ...
- 找xpath好用的工具(比较少用,针对只能在IE上打开的网站)
有一些网站只能在IE浏览器里打开,不像firefox那样有好多好用的插件来找元素的xpath,css path等. 当然现在IE也可以,F12出现像firebug那样的窗口,来查看元素. 这里呢在介绍 ...