[The C++ Programming Language에서 발췌]


    표준 라이브러리는 시간을 다루기 위한 기능을 제공한다. 예를 들면 뭔가의 시간을 맞추는 기본적인 방법은 다음과 같다.

     

    1
    2
    3
    4
    5
    6
    using namespace std::chorono;
     
    auto t0 = high_resolution_clock::now();
    do_work();
    auto t1 = high_resolution_clock::now();
    cout << duration_cast<milliseconds>(t1 - t0).count() << "msec\n";;
    cs


    시계는 time_point(시간상 어떤 지점)를 반환한다. 두 개의 time_point로 뺄셈을 수행하면 duration(시간의 기간)이 얻어진다. 다양한 시계들이 다양한 시간의 단위로 결과를 보여주므로(여기서 사용한 시계는 nameseconds 단위를 사용했다), 대체적으로 duration을 알려진 단위로 변환하는 편이 바람직하다. duration_cast가 하는 일이 발 그것이다.

    시간을 다루는 표준 라이브러리 기능은 <chrono>에 들어 있는 부분 네임스페이스 std::chrono에서 찾을 수 있다.

    먼저 시간을 측정해보지도 않고 코드의 '효율성'에 대해 단정하지 말기 바란다. 성능에 대한 측정은 대부분 신뢰하기 힘들다.


    [예제 코드]


    [진행 시간 출력]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    #include <iostream>
    #include <chrono>
    using namespace std::chrono;
    using std::cout;
    using std::endl;
     
    auto main(void-> int
    {
        steady_clock::time_point startTime = steady_clock::now(); // 물리적 고정 시간
        // system_clock::time_point startTime = system_clock::now(); 컴퓨터 시스템 시간
     
        while (true)
        {
            steady_clock::time_point endTime = steady_clock::now();
            duration<double> currentTime = endTime - startTime;
            cout << currentTime.count() << endl;
        }
    }
    cs


    [시간을 초 단위로 출력]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #include <iostream>
    #include <chrono>
    using namespace std::chrono;
    using std::cout;
    using std::endl;
     
    auto main(void-> int
    {
        steady_clock::time_point startTime = steady_clock::now(); // 물리적 고정 시간
        // system_clock::time_point startTime = system_clock::now(); 컴퓨터 시스템 시간
     
        while (true)
        {
            steady_clock::time_point endTime = steady_clock::now();
            seconds currentSeconds = duration_cast<seconds>(endTime - startTime);
            // duration_cast<seconds> 시간을 초 단위로 캐스팅
            cout << currentSeconds.count() << endl;
        }
    }
    cs


    [시간을 분 단위로 출력]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #include <iostream>
    #include <chrono>
    using namespace std::chrono;
    using std::cout;
    using std::endl;
     
    auto main(void-> int
    {
        steady_clock::time_point startTime = steady_clock::now(); // 물리적 고정 시간
        // system_clock::time_point startTime = system_clock::now(); 컴퓨터 시스템 시간
     
        while (true)
        {
            steady_clock::time_point endTime = steady_clock::now();
            minutes currentSeconds = duration_cast<minutes>(endTime - startTime);
            // duration_cast<minutes> 시간을 분 단위로 캐스팅
            cout << currentSeconds.count() << endl;
        }
    }
    cs


    [시간을 시 단위로 출력]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #include <iostream>
    #include <chrono>
    using namespace std::chrono;
    using std::cout;
    using std::endl;
     
    auto main(void-> int
    {
        steady_clock::time_point startTime = steady_clock::now(); // 물리적 고정 시간
        // system_clock::time_point startTime = system_clock::now(); 컴퓨터 시스템 시간
     
        while (true)
        {
            steady_clock::time_point endTime = steady_clock::now();
            hours currentSeconds = duration_cast<hours>(endTime - startTime);
            // duration_cast<minutes> 시간을 시 단위로 캐스팅
            cout << currentSeconds.count() << endl;
        }
    }
    cs

     

    [나노초 단위로 출력] 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #include <iostream>
    #include <chrono>
    using namespace std::chrono;
    using std::cout;
    using std::endl;
     
    auto main(void-> int
    {
        steady_clock::time_point startTime = steady_clock::now(); // steady_clock은 물리적 시간
        // system_clock::time_point startTime = system_clock::now(); 컴퓨터 시스템 시간
     
        while (true)
        {
            steady_clock::time_point endTime = steady_clock::now();
            nanoseconds currentSeconds = duration_cast<nanoseconds>(endTime - startTime);
            // duration_cast<minutes> 시간을 나노초 단위로 캐스팅
            cout << currentSeconds.count() << endl;
        }
    }
    cs


    [high_resolution_clock과 밀리초 단위로 출력]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #include <iostream>
    #include <chrono>
    using namespace std::chrono;
    using std::cout;
    using std::endl;
     
    auto main(void-> int
    {
        high_resolution_clock::time_point startTime = high_resolution_clock::now();
        // high_resolution_clock는 해당 플랫폼(Windows, Mac 등)에서 가장 짧은 단위의 시간
     
        while (true)
        {
            high_resolution_clock::time_point endTime = high_resolution_clock::now();
            milliseconds currentSeconds = duration_cast<milliseconds>(endTime - startTime);
            // duration_cast<minutes> 시간을 밀리세컨드 단위로 캐스팅
            cout << currentSeconds.count() << endl;
        }
    }
    cs


    Posted by Muramasa