[모던C++입문] 4.5 지금 시각은?

4.5 지금 시각은?

  • <chrono> 라이브러리는 타입 세이프한 시계 및 타이머 기능을 제공
    • 클럭을 기준으로 특정 시점을 나타내는 time_point
    • 명백한 의미를 지닌 duration
time_point<system_clock> now = system_clock::now(), then = now + hours(2);
time_t then_time = system_clock::to_time_t(then);
cout << ctime(&then_time);  //endl이 포함되어 있음
  • 어떤 계산이 얼마나 오래 걸렸는지를 구할 수 있다.
inline double my_root(double x, double eps = 1e-12)
{
    double sq = 1.0, sqo;
    do{
        sqo = sq;
        sq = 0.5 * (sqo + x / sqo);
    } while (abs(sq - sqo) > eps);
    return sq;
}

time_point<teady_clock> start = steady_clock::now();
for( int i = 0; i < rep; i++)
{
    r3 = my_root(3.0);
}
auto end = steady_clock::now();

cout << "my_root(3.0) = " << r3 << endl;
cout << ((end - start) / rep).count() << " thicks" << endl;
//my_root(3.0) = 1.73205
//54 thicks

cout << duration_cast<microseconds>((end - start) / rep).count() << endl;
//0

cout << duration_cast<microseconds>((end - start) / rep).count() / 1000. << endl;
//0.054
  • 클록의 해상도는 1나노초이다.
    • system_clock은 <ctime>과 호환 가능하다
    • high_resolution_clock은 가능한 최대 해상도를 갖는다.
    • steady_clock은 증가하는 시점을 보장하는 시계이다.
반응형

댓글

Designed by JB FACTORY