cpu_timer_info.cpp 2.1 KB

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