1 #include <iostream> 2 #include <chrono> 3 #include <ratio> 4 5 #if !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) 6 // In GCC 4.6 and below, "steady_clock" is called "monotonic_clock", 7 // and "is_steady" is called "is_monotonic" 8 namespace std { 9 namespace chrono { 10 typedef monotonic_clock steady_clock; 11 } 12 } 13 #define is_steady is_monotonic 14 #endif 15 16 template <typename C> 17 void printClockData (bool &is_high_res, bool &is_steady) 18 { 19 using namespace std; 20 21 cout << "- precision: "; 22 // if time unit is less or equal one millisecond 23 typedef typename C::period P;// type of time unit 24 if (ratio_less_equal<P,milli>::value) { 25 // convert to and print as milliseconds 26 typedef typename ratio_multiply<P,kilo>::type TT; 27 cout << fixed << double(TT::num)/TT::den 28 << " milliseconds" << endl; 29 is_high_res = true; 30 } 31 else { 32 // print as seconds 33 cout << fixed << double(P::num)/P::den << " seconds" << endl; 34 is_high_res = false; 35 } 36 cout << "- is_steady: " << boolalpha << C::is_steady << endl; 37 is_steady = C::is_steady; 38 } 39 40 int main() 41 { 42 bool is_high_res1, is_high_res2, is_high_res3, is_steady; 43 std::cout << "system_clock: " << std::endl; 44 printClockData<std::chrono::system_clock>(is_high_res1, is_steady); 45 std::cout << "\nhigh_resolution_clock: " << std::endl; 46 printClockData<std::chrono::high_resolution_clock>(is_high_res2, is_steady); 47 std::cout << "\nsteady_clock: " << std::endl; 48 printClockData<std::chrono::steady_clock>(is_high_res3, is_steady); 49 50 return (is_high_res1 && is_high_res2 && is_high_res3 && is_steady)? 0 : 1; 51 } 52