original_timer_test.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // timer, job_timer, and progress_display sample program -------------------//
  2. // Copyright Beman Dawes 1998. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/timer for documentation.
  6. // Revision History
  7. // 12 Jan 01 Cut time to 1.0 secs to speed regression tests (Beman Dawes)
  8. // 25 Sep 99 added elapsed_min() and elapsed_max() reporting
  9. // 16 Jul 99 Second beta
  10. // 6 Jul 99 Initial boost version
  11. #include <boost/progress.hpp>
  12. #include <iostream>
  13. #include <climits>
  14. using boost::timer;
  15. using boost::progress_timer;
  16. using boost::progress_display;
  17. using std::cout;
  18. using std::endl;
  19. int main() {
  20. timer t0; // used only for elapsed_max() and elapsed_min()
  21. cout << "timer::elapsed_min() reports " << t0.elapsed_min() << " seconds\n";
  22. cout << "timer::elapsed_max() reports " << t0.elapsed_max()
  23. << " seconds, which is " << t0.elapsed_max()/3600.0 << " hours\n";
  24. cout << "\nverify progress_display(0) doesn't divide by zero" << endl;
  25. progress_display zero( 0 ); // verify 0 doesn't divide by zero
  26. ++zero;
  27. long loops;
  28. timer loop_timer;
  29. const double time = 1.0;
  30. cout << "\ndetermine " << time << " second iteration count" << endl;
  31. for ( loops = 0; loops < LONG_MAX
  32. && loop_timer.elapsed() < time; ++loops ) {}
  33. cout << loops << " iterations"<< endl;
  34. long i;
  35. bool time_waster; // defeat [some] optimizers by storing result here
  36. progress_timer pt;
  37. timer t1;
  38. timer t4;
  39. timer t5;
  40. cout << "\nburn about " << time << " seconds" << endl;
  41. progress_display pd( loops );
  42. for ( i = loops; i--; )
  43. { time_waster = loop_timer.elapsed() < time; ++pd; }
  44. timer t2( t1 );
  45. timer t3;
  46. t4 = t3;
  47. t5.restart();
  48. cout << "\nburn about " << time << " seconds again" << endl;
  49. pd.restart( loops );
  50. for ( i = loops; i--; )
  51. { time_waster = loop_timer.elapsed() < time; ++pd; }
  52. if ( time_waster ) cout << ' '; // using time_waster quiets compiler warnings
  53. progress_display pd2( 50, cout, "\nLead string 1 ", "Lead string 2 ", "Lead string 3 " );
  54. for ( ; pd2.count() < 50; ++pd2 ) {}
  55. cout << "\nt1 elapsed: " << t1.elapsed() << '\n';
  56. cout << "t2 elapsed: " << t2.elapsed() << '\n';
  57. cout << "t3 elapsed: " << t3.elapsed() << '\n';
  58. cout << "t4 elapsed: " << t4.elapsed() << '\n';
  59. cout << "t5 elapsed: " << t5.elapsed() << '\n';
  60. cout << "t1 and t2 should report the same times (very approximately "
  61. << 2*time << " seconds).\n";
  62. cout << "t3, t4 and t5 should report about the same times,\n";
  63. cout << "and these should be about half the t1 and t2 times.\n";
  64. cout << "The following elapsed time should be slightly greater than t1."
  65. << endl;
  66. return 0;
  67. } // main