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