ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 프로그램 실행시간 측정(Running time)
    Develope/C++ 2020. 4. 2. 10:38

    chrono 라이브러리를 활용한 프로그램 실행시간 측정코드

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <random>
    #include <chrono>
    
    using namespace std;
    
    class Timer
    {
        using clock_t = std::chrono::high_resolution_clock;
        using second_t = std::chrono::duration<double, std::ratio<1>>;
    
        std::chrono::time_point<clock_t> start_time = clock_t::now();
    
    public:
        void elapsed()
        {
            std::chrono::time_point<clock_t> end_time = clock_t::now();
    
            cout << std::chrono::duration_cast<second_t>(end_time - start_time).count() << endl;
        }
    };
    
    int main()
    {
        random_device rnd_device;
        mt19937 mersenne_engine{ rnd_device() };
    
        vector<int> vec(10000);
        for (unsigned int i = 0; i < vec.size(); ++i)
            vec[i] = i;
    
        std::shuffle(begin(vec), end(vec), mersenne_engine);
    
        //for (auto& e : vec)
        //    cout << e << " ";
        //cout << endl;
    
        Timer timer;
    
        std::sort(begin(vec), end(vec));
    
        timer.elapsed();
    
        //for (auto& e : vec)
        //    cout << e << " ";
        //cout << endl;
    }

    댓글

Designed by Tistory.