标签:www 方式 tick class solution time_t nano minutes stars
所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解。
chrono是一个time library, 源于boost,现在已经是C++标准。话说今年似乎又要出新标准了,好期待啊!
要使用chrono库,需要#include<chrono>,其所有实现均在std::chrono namespace下。注意标准库里面的每个命名空间代表了一个独立的概念。所以下文中的概念均以命名空间的名字表示! chrono是一个模版库,使用简单,功能强大,只需要理解三个概念:duration、time_point、clock
1 template <class Rep, class Period = ratio<1> > class duration;
1 template <intmax_t N, intmax_t D = 1> class ratio;
1 template <class ToDuration, class Rep, class Period> 2 constexpr ToDuration duration_cast (const duration<Rep,Period>& dtn);
1 // duration constructor 2 #include <iostream> 3 #include <ratio> 4 #include <chrono> 5 6 int main () 7 { 8 typedef std::chrono::duration<int> seconds_type; 9 typedef std::chrono::duration<int,std::milli> milliseconds_type; 10 typedef std::chrono::duration<int,std::ratio<60*60>> hours_type; 11 12 hours_type h_oneday (24); // 24h 13 seconds_type s_oneday (60*60*24); // 86400s 14 milliseconds_type ms_oneday (s_oneday); // 86400000ms 15 16 seconds_type s_onehour (60*60); // 3600s 17 //hours_type h_onehour (s_onehour); // NOT VALID (type truncates), use: 18 hours_type h_onehour (std::chrono::duration_cast<hours_type>(s_onehour)); 19 milliseconds_type ms_onehour (s_onehour); // 3600000ms (ok, no type truncation) 20 21 std::cout << ms_onehour.count() << "ms in 1h" << std::endl; 22 23 return 0; 24 } 25 26 duration还有一个成员函数count()返回Rep类型的Period数量,看代码: 27 28 // duration::count 29 #include <iostream> // std::cout 30 #include <chrono> // std::chrono::seconds, std::chrono::milliseconds 31 // std::chrono::duration_cast 32 33 int main () 34 { 35 using namespace std::chrono; 36 // std::chrono::milliseconds is an instatiation of std::chrono::duration: 37 milliseconds foo (1000); // 1 second 38 foo*=60; 39 40 std::cout << "duration (in periods): "; 41 std::cout << foo.count() << " milliseconds.\n"; 42 43 std::cout << "duration (in seconds): "; 44 std::cout << foo.count() * milliseconds::period::num / milliseconds::period::den; 45 std::cout << " seconds.\n"; 46 47 return 0; 48 }
1 template <class Clock, class Duration = typename Clock::duration> class time_point;
1 // time_point constructors 2 #include <iostream> 3 #include <chrono> 4 #include <ctime> 5 6 int main () 7 { 8 using namespace std::chrono; 9 10 system_clock::time_point tp_epoch; // epoch value 11 12 time_point <system_clock,duration<int>> tp_seconds (duration<int>(1)); 13 14 system_clock::time_point tp (tp_seconds); 15 16 std::cout << "1 second since system_clock epoch = "; 17 std::cout << tp.time_since_epoch().count(); 18 std::cout << " system_clock periods." << std::endl; 19 20 // display time_point: 21 std::time_t tt = system_clock::to_time_t(tp); 22 std::cout << "time_point tp is: " << ctime(&tt); 23 24 return 0; 25 } 26
1 template <class ToDuration, class Clock, class Duration> 2 time_point<Clock,ToDuration> time_point_cast (const time_point<Clock,Duration>& tp);
1 / time_point_cast 2 #include <iostream> 3 #include <ratio> 4 #include <chrono> 5 6 int main () 7 { 8 using namespace std::chrono; 9 10 typedef duration<int,std::ratio<60*60*24>> days_type; 11 12 time_point<system_clock,days_type> today = time_point_cast<days_type>(system_clock::now()); 13 14 std::cout << today.time_since_epoch().count() << " days since epoch" << std::endl; 15 16 return 0; 17 }
1 // system_clock example 2 #include <iostream> 3 #include <ctime> 4 #include <ratio> 5 #include <chrono> 6 7 int main () 8 { 9 using std::chrono::system_clock; 10 11 std::chrono::duration<int,std::ratio<60*60*24> > one_day (1); 12 13 system_clock::time_point today = system_clock::now(); 14 system_clock::time_point tomorrow = today + one_day; 15 16 std::time_t tt; 17 18 tt = system_clock::to_time_t ( today ); 19 std::cout << "today is: " << ctime(&tt); 20 21 tt = system_clock::to_time_t ( tomorrow ); 22 std::cout << "tomorrow will be: " << ctime(&tt); 23 24 return 0; 25 } 26
1 // steady_clock example 2 #include <iostream> 3 #include <ctime> 4 #include <ratio> 5 #include <chrono> 6 7 int main () 8 { 9 using namespace std::chrono; 10 11 steady_clock::time_point t1 = steady_clock::now(); 12 13 std::cout << "printing out 1000 stars...\n"; 14 for (int i=0; i<1000; ++i) std::cout << "*"; 15 std::cout << std::endl; 16 17 steady_clock::time_point t2 = steady_clock::now(); 18 19 duration<double> time_span = duration_cast<duration<double>>(t2 - t1); 20 21 std::cout << "It took me " << time_span.count() << " seconds."; 22 std::cout << std::endl; 23 24 return 0; 25 } 26
标签:www 方式 tick class solution time_t nano minutes stars
原文地址:http://www.cnblogs.com/alenoscar/p/7067129.html