首页 > 编程语言 > 详细

C++基于范围的for循环性能测试(针对std::vector)

时间:2017-12-03 23:41:57      阅读:424      评论:0      收藏:0      [点我收藏+]

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)

原文:http://www.cnblogs.com/KunLunSu/p/7967643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!