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、运行结果如下:

 

posted @ 2017-12-03 22:18  自由真实个性  阅读(1587)  评论(0编辑  收藏  举报