zoukankan      html  css  js  c++  java
  • 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、运行结果如下:

  • 相关阅读:
    E. You Are Given Some Strings...
    神奇函数
    AC自动机再加强版
    AC自动机2
    AC自动机
    three arrays
    permutation 2
    string matching
    permutation 1
    equation
  • 原文地址:https://www.cnblogs.com/KunLunSu/p/7967643.html
Copyright © 2011-2022 走看看