C++基于范围的for循环性能测试(针对std::vector)
1、代码如下:
void output1(int x)
{
if (x == 10000000)
{
std::cout << x << std::endl;
}
}
const std::string getCurrentSystemTime()
{
auto tt = std::chrono::system_clock::to_time_t
(std::chrono::system_clock::now());
struct tm* ptm = localtime(&tt);
char date[60] = { 0 };
sprintf(date, "%d-%02d-%02d %02d:%02d:%02d",
(int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday,
(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
return std::string(date);
}
void Test9()
{
std::cout << getCurrentSystemTime() << std::endl;
std::vector<int> coll;
for (int i = 0; i <= 10000000; i++)
{
coll.push_back(i);
}
std::cout<<getCurrentSystemTime()<<std::endl;
std::for_each(coll.begin(), coll.end(), output1);
std::cout << getCurrentSystemTime() << std::endl;
for (auto iter : coll)
{
if (iter == 10000000)
{
std::cout << iter << std::endl;
}
}
std::cout << getCurrentSystemTime() << std::endl;
for (auto iter = coll.begin(); iter != coll.end(); ++iter)
{
if (*iter == 10000000)
{
std::cout << *iter << std::endl;
}
}
std::cout << getCurrentSystemTime() << std::endl;
for (auto iter = coll.begin(); iter != coll.end(); iter++)
{
if (*iter == 10000000)
{
std::cout << *iter << std::endl;
}
}
std::cout << getCurrentSystemTime() << std::endl;
}
2、运行结果如下:
C++基于范围的for循环性能测试(针对std::vector)的更多相关文章
- C++11基于范围的for循环
C++11包含一种新的 for 循环,称为基于范围的 for 循环,可以简化对数组元素的遍历.格式如下: for(Type VarName : Array){ //每个元素的值会依次赋给 VarNam ...
- Atitit.升级软件的稳定性---基于数据库实现持久化 循环队列 循环队列
Atitit.升级软件的稳定性---基于数据库实现持久化 循环队列 环形队列 1. 前言::选型(马) 1 2. 实现java.util.queue接口 1 3. 当前指针的2个实现方式 1 1.1 ...
- C++ 11 学习2:空指针(nullptr) 和 基于范围的for循环(Range-based for loops)
3.空指针(nullptr) 早在 1972 年,C语言诞生的初期,常数0带有常数及空指针的双重身分. C 使用 preprocessor macroNULL 表示空指针, 让 NULL 及 0 分别 ...
- 基于范围的for循环(STL)
1. ]={4.99,5.99,6.99,7.99,8.99}; for (double x : prices) cout<<x<<endl; //////////////// ...
- 基于范围的for循环(C++11)
C++11新增了一种循环:基于范围的for循环.这简化了一种常见的循环任务:对数组(或容器类,如vector和array)的每个元素执行相同的操作,如下例所示 for语句允许简单的范围迭代:(只遍历, ...
- 第9课 基于范围的for循环
1. 基于范围的for循环(range-based for) (1)语法:for(decl : coll){//statement} ①decl用于声明元素及类型,如int elem或auto ele ...
- c++11 基于范围的for循环
c++11 基于范围的for循环 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> # ...
- c++11——基于范围的for循环
c++11中有基于范围的for循环,基于范围的for循环可以不再关心迭代器的概念,只需要关系容器中的元素类型即可,同时也不必显式的给出容器的开头和结尾. int arr[] = {1, 2, 3, 4 ...
- c++11之一: 基于范围的for循环
#include <iostream> using namespace std; int main(){ ]{,,,,}; for (int& e: ary) e *= ; for ...
随机推荐
- 关于ajax访问webservice查询数据量稍微大一点,就会返回error500的解决方案
只需要在web.config的configeration节点中增加如下子节点即可: <system.web.extensions> <scripting> ...
- Python isinstance 与 instance 的用法
instance: instance 属于python2的关键字,python2中如果一个类没有继承自object, 那么实例化出来的对象就是instance类型,否则就是class类型. isins ...
- [海外干货] BlackHat 2017 首日议题的所有 PPT以及材料
今年是 Black Hat 举办的第 20 个年头,高温酷暑也挡不住全世界黑客和安全人员奔赴拉斯维加斯的热情.毕竟这可是一年一度的盛大狂欢啊.今年的 BHUSA 从美国东部时间时间 7 月 22 日( ...
- Swift5 语言指南(二十三) 协议
甲协议定义的该适合特定任务或片的功能的方法,属性和其他要求的蓝图.然后,可以通过类,结构或枚举来采用该协议,以提供这些要求的实际实现.任何满足协议要求的类型都被认为符合该协议. 除了指定符合类型必须实 ...
- angularJS详解
这篇文章转载 kooky798 的博客,http://blog.csdn.net/yy374864125/article/details/41349417, 写到这么详细也是没谁了,必须粉一个 1 ...
- Angular使用总结 --- 搜索场景中使用rxjs的操作符
在有input输入框的搜索/过滤业务中,总会考虑如何减少发起请求频率,尽量使每次的请求都是有效的.节流和防抖是比较常见的做法,这类函数的实现方式也不难,不过终归还是需要自己封装.rxjs提供了各种操作 ...
- maven build的常用生命周期
常用的maven build goals: validate - validate the project is correct and all necessary information is av ...
- BIND DNS拒绝服务漏洞 CVE-2016-2776修复
接到此漏洞之后,略微查了一下相关描述,发现漏洞影响范围很大,可能造成的影响也很严重,于是着手进行修复. 漏洞的详细信息可见如下链接: http://www.cnvd.org.cn/flaw/show/ ...
- idea 运行scala代码 报错:Exception in thread "main" java.lang.NoClassDefFoundError: scala/Predef$ java.lang.NoClassDefFoundError: scala/Function0 Error: A JNI error has occurred, please check your installati
各种报错信息如下: java.lang.NoClassDefFoundError: scala/Function0 at java.lang.Class.getDeclaredMethods0(Nat ...
- Activity四大启动模式
ctivity的四种启动模式: standard.singleTop.singleTask.singleInstance 为了打印方便,定义一个基础Activity,在其onCreate方法和onNe ...