The so-called explanation is only a reference to the www.cplusplus.com of the description of the collation, because did not find that others have detailed explanation.
Chrono is a time library, derived from boost, and is now a C + + standard. This year seems to be a new standard, so look forward to Ah!
To use the Chrono Library, #include<chrono> is required, and all of its implementations are under Std::chrono namespace. Note that each namespace inside the standard library represents an independent concept. So the concepts in the following are represented by the name of the namespace! Chrono is a template library, easy to use and powerful, with only three concepts to understand: Duration, time_point, clock
1.durationsstd::chrono::d uration for a period of time, such as two hours, 12.88 seconds, half an hour, a moxa fragrant time and so on, as long as can be converted into seconds.
1 Template <class Rep, class Period = ratio<1> > class duration;
Where Rep represents a numeric type used to represent the number of period, such as an int float doubleperiod is a ratio type, used to denote "units of time in seconds" such as second milisecond commonly used Duration<rep ,period> already defined, under Std::chrono::d uration: ratio<3600, 1> hoursratio<60, 1> m Inutesratio<1, 1> secondsratio<1, 1000> microsecondsratio<1, 1000000> Microsecondsratio<1, 1000000000> nanosecons Here you need to explain the prototype of ratio this kind of template:
1 template <intmax_t N, intmax_t D = 1> class ratio;
n represents the numerator, D represents the denominator, so ratio represents a fractional value. Note that we can define the period ourselves, such as ratio<1, and the -2> indicates that the unit time is-0.5 seconds. Because of various duration representations, the Chrono Library provides a duration_cast type conversion function.
1 Template <class Toduration, class Rep, class period>2 constexpr toduration duration_cast (const DURATION<R ep,period>& DTN);
A typical usage is to represent a period of time:
1//Duration constructor 2 #include <iostream> 3 #include <ratio> 4 #include <chrono> 5 6 int Main ( ) 7 {8 typedef std::chrono::d uration<int> seconds_type; 9 typedef std::chrono::d uration<int,std::milli> m illiseconds_type;10 typedef std::chrono::d uration<int,std::ratio<60*60>> hours_type;11 hours_type h_o Neday (24); 24h13 seconds_type S_oneday (60*60*24); 86400s14 milliseconds_type Ms_oneday (s_oneday); 86400000ms15 seconds_type S_onehour (60*60); 3600s17//hours_type H_onehour (s_onehour); Not VALID (type truncates), use:18 hours_type h_onehour (std::chrono::d uration_cast2. Timepoints Std::chrono::time_point represents a specific time, such as the 80 's, your birthday, this afternoon, the train departure time, etc., as long as it can be indicated by a computer clock. Given the circumstances of our use of time, the specific extent of this point is determined by the chosen unit. A time point must have a clock timing. See clock's description.1 Template <class clock, class Duration = TypeName Clock::d uration> class Time_point;
The following are examples of constructs using Time_point:1//Time_point Constructors 2 #include <iostream> 3 #include <chrono> 4 #include <ctime> 5 6 int ma In () 7 {8 using namespace Std::chrono; 9 system_clock::time_point Tp_epoch; Epoch Value11 time_point <system_clock,duration<int>> tp_seconds (duration<int> (1)) ; system_clock::time_point TP (tp_seconds), std::cout << "1 second since system_ Clock epoch = "; std::cout << Tp.time_since_epoch (). Count (); std::cout <<" System_clock Periods. "<< std::endl;19 //display time_point:21 std::time_t tt = system_clock::to_time_t ( TP); std::cout << "time_point TP is:" << CTime (&TT), return 0;25}26
Time_point has a function time_from_eproch () to obtain duration from January 1, 1970 to Time_point time. For example, if Timepoint is in days, the duration returned by the function is in days. Because of the different time_point representations, Chrono also provides the corresponding conversion function Time_point_cast.1 Template <class Toduration, class Clock, class duration>2 time_point<clock,toduration> time_point_ CAST (const time_point<clock,duration>& TP);
such as calculation/1/time_point_cast 2 #include <iostream> 3 #include <ratio> 4 #include <chrono> 5 6 int main () 7 { 8 using namespace Std::chrono; 9 typedef duration<int,std::ratio<60*60*24>> DAYS_TYPE;11 time_point<system_clock,days_type> today = time_point_cast<days_type> (System_clock::now () ); std::cout << Today.time_since_epoch (). Count () << days since epoch << std::endl;15< C10/>16 return 0;17}
3.Clocks Std::chrono::system_clock It represents the current system clock, and all processes running in the system use now () to get the same time. Each clock class has a deterministic time_point, duration, Rep, period type. The operation is: Now () Current time time_pointto_time_t () Time_point converted to time_t seconds from_time_t () from time_t to time_point Typical application is calculated time Date:
1//System_clock Example 2 #include <iostream> 3 #include <ctime> 4 #include <ratio> 5 #include <CHR Ono> 6 7 int main () 8 {9 using std::chrono::system_clock;10 one- std::chrono::d uration<int,std:: ratio<60*60*24> > One_day (1); System_clock::time_point today = System_clock::now () System_clock::time_point tomorrow = Today + one_day;15 std::time_t tt;17 tt = system_clock::to_ time_t (today) std::cout << "Today is:" << CTime (&TT); tt = System_clock::to_ti Me_t (Tomorrow), std::cout << "Tomorrow'll be:" << CTime (&TT), 0;25}26
Std::chrono::steady_clock in order to represent a stable interval, the last call to now () will always get a greater time than the previous value (the meaning of this sentence is that if the system time is modified halfway, it does not affect the result of now (). Each tick is guaranteed to have a stable time interval. The operation has: Now () gets the current clock typical application is to give the algorithm timing:1//Steady_clock Example 2 #include <iostream> 3 #include <ctime> 4 #include <ratio> 5 #include <CHR Ono> 6 7 int main () 8 {9 using namespace std::chrono;10 one steady_clock::time_point t1 = Steady_clock :: Now (), std::cout << "printing out stars...\n"; + for (int i=0; i<1000; ++i) Std::cout &L t;< "*"; std::cout << std::endl;16 steady_clock::time_point t2 = Steady_clock::now (); 18 duration<double> time_span = duration_cast<duration<double>> (T2-T1); 21 std::cout << "It took me" << time_span.count () << "seconds."; std::cout << std::endl;23 return 0;25}26
The last clock, std::chrono::high_resolution_clock as the name implies, is the highest precision clock available to the system. In fact High_resolution_clock is only a typedef of SYSTEM_CLOCK or Steady_clock. The action is: now () gets the current clock.C++11 Std::chrono Library Detailed