C++의 크로노
system_clock은 시스템 전체의 실시간 벽시계를 나타냅니다. 시스템의 시간 조정에 영향을 받습니다.
steady_clock은 시스템 시간 변경에 영향을 받지 않고 단조롭게 증가하는 시계를 나타냅니다.
high_solution_clock은 시스템에서 사용 가능한 가장 짧은 틱 주기를 갖는 시계입니다.
추가적으로
Chrono 라이브러리는 날짜와 시간을 처리하는 데 사용됩니다. 이 라이브러리는 타이머와 시계가 시스템마다 다를 수 있다는 사실을 처리하여 시간이 지남에 따라 정밀도 측면에서 향상되도록 설계되었습니다. 크로노의 독특한 점은 특정 시계에서 지속 시간과 시점(timepoint)을 분리하여 정밀 중립 개념을 구현합니다. chrono는 헤더의 이름이자 하위 네임스페이스의 이름입니다. 이 헤더의 모든 요소(common_type 특수화 제외)는 std 네임스페이스(대부분의 표준 라이브러리와 마찬가지로) 바로 아래에 정의되지 않고 std::chrono 네임스페이스 . 이 헤더의 요소는 시간을 다룹니다. 이는 주로 세 가지 개념을 통해 수행됩니다.
지속
기간 객체는 1분, 2시간 또는 10밀리초와 같은 수로 시간 범위를 표현합니다. 예를 들어 '42초'는 1초 시간 단위의 42틱으로 구성된 기간으로 표시될 수 있습니다.
CPP // C++ program to illustrate the utility // function duration::count #include #include int main () { using namespace std :: chrono ; // std::chrono::milliseconds is an // instantiation of std::chrono::duration:- 1 second milliseconds mil ( 1000 ); mil = mil * 60 ; std :: cout & lt ; & lt ; & quot ; duration ( in periods ) : & quot ;; std :: cout & lt ; & lt ; mil . count () & lt ; & lt ; & quot ; milliseconds . n & quot ;; std :: cout & lt ; & lt ; & quot ; duration ( in seconds ) : & quot ;; std :: cout & lt ; & lt ; ( mil . count () * milliseconds :: period :: num / milliseconds :: period :: den ); std :: cout & lt ; & lt ; & quot ; seconds . n & quot ;; return 0 ; }
산출:
duration (in periods): 60000 milliseconds. duration (in seconds): 60 seconds.
시계
시계는 시작점(또는 에포크)과 틱 속도로 구성됩니다. 예를 들어 시계의 시점은 1996년 2월 22일이고 매초마다 똑딱거릴 수 있습니다. C++에서는 세 가지 시계 유형을 정의합니다.
시점
time_point 개체는 시계의 신기원을 기준으로 특정 시점을 표현합니다. 내부적으로 객체는 기간 유형의 객체를 저장하고 시계 유형을 해당 에포크에 대한 참조로 사용합니다.
CPP // C++ program to illustrate time point // and system clock functions #include #include #include // Function to calculate // Fibonacci series long fibonacci ( unsigned n ) { if ( n & lt ; 2 ) return n ; return fibonacci ( n -1 ) + fibonacci ( n -2 ); } int main () { // Using time point and system_clock std :: chrono :: time_point & lt ; std :: chrono :: system_clock & gt ; start end ; start = std :: chrono :: system_clock :: now (); std :: cout & lt ; & lt ; & quot ; f ( 42 ) = & quot ; & lt ; & lt ; fibonacci ( 42 ) & lt ; & lt ; 'n' ; end = std :: chrono :: system_clock :: now (); std :: chrono :: duration & lt ; double & gt ; elapsed_seconds = end - start ; std :: time_t end_time = std :: chrono :: system_clock :: to_time_t ( end ); std :: cout & lt ; & lt ; & quot ; finished computation at & quot ; & lt ; & lt ; std :: ctime ( & amp ; end_time ) & lt ; & lt ; & quot ; elapsed time : & quot ; & lt ; & lt ; elapsed_seconds . count () & lt ; & lt ; & quot ; s n & quot ;; }
산출:
f(42) = 267914296 finished computation at Wed Jan 4 05:13:48 2017 elapsed time: 2.14538s
다음에서 제공하는 시계 및 지속 시간의 정밀도와 정확성을 염두에 두는 것이 중요합니다.
퀴즈 만들기