码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-12-04 00:04:15      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:clock   for_each   ons   运行   c++   性能测试   har   void   print   

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)

标签:clock   for_each   ons   运行   c++   性能测试   har   void   print   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!