1 // boost cpu_timer_info.cpp ----------------------------------------------------------// 2 3 // Copyright Beman Dawes 2011 4 5 // Distributed under the Boost Software License, Version 1.0. 6 // See http://www.boost.org/LICENSE_1_0.txt 7 8 // See http://www.boost.org/libs/timer for documentation. 9 10 #include <boost/timer/timer.hpp> 11 #include <boost/chrono/chrono.hpp> 12 #include <boost/detail/lightweight_main.hpp> 13 #include <cstdlib> // for atol() 14 #include <iostream> 15 #include <locale> 16 17 using boost::timer::nanosecond_type; 18 using boost::timer::cpu_times; 19 using boost::timer::cpu_timer; 20 using boost::timer::auto_cpu_timer; 21 using std::cout; using std::endl; 22 cpp_main(int argc,char * argv[])23int cpp_main( int argc, char * argv[] ) 24 { 25 cout << '\n'; 26 cout << "For cpu_times.wall, the underlying clock " 27 << (boost::chrono::high_resolution_clock::is_steady 28 ? "is steady. " 29 : "is not steady. " 30 ) 31 << "Steady clocks are defined by C++11 as clocks for which values " 32 "of time_point never decrease as physical time advances and for " 33 "which values of time_point advance at a steady rate relative to " 34 "real time. That is, the clock may not be adjusted.\n\n"; 35 36 cpu_times start_time; 37 start_time.clear(); 38 cpu_times current_time; 39 40 { 41 cpu_timer cpu; 42 cout << "measure boost::timer::cpu_timer resolution for user time..." 43 << endl; 44 for (int i = 0; i < 3; ++i) 45 { 46 cpu.start(); 47 start_time = cpu.elapsed(); 48 current_time.user = start_time.user; 49 while (current_time.user == start_time.user) 50 { 51 current_time = cpu.elapsed(); 52 } 53 cout << current_time.user - start_time.user << "ns\n"; 54 } 55 } 56 57 { 58 cpu_timer cpu; 59 cout << "measure boost::timer::cpu_timer resolution for wall-clock time..." 60 << endl; 61 for (int i = 0; i < 100; ++i) 62 { 63 cpu.start(); 64 start_time.wall = cpu.elapsed().wall; 65 current_time.wall = start_time.wall; 66 while (current_time.wall == start_time.wall) 67 { 68 current_time.wall = cpu.elapsed().wall; 69 } 70 cout << current_time.wall - start_time.wall << "ns "; 71 } 72 } 73 return 0; 74 } 75 76