[29] | 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 | #include <boost/progress.hpp> |
---|
| 16 | #include <iostream> |
---|
| 17 | #include <climits> |
---|
| 18 | |
---|
| 19 | using boost::timer; |
---|
| 20 | using boost::progress_timer; |
---|
| 21 | using boost::progress_display; |
---|
| 22 | using std::cout; |
---|
| 23 | using std::endl; |
---|
| 24 | |
---|
| 25 | int main() { |
---|
| 26 | |
---|
| 27 | timer t0; // used only for elapsed_max() and elapsed_min() |
---|
| 28 | |
---|
| 29 | cout << "timer::elapased_min() reports " << t0.elapsed_min() << " seconds\n"; |
---|
| 30 | cout << "timer::elapased_max() reports " << t0.elapsed_max() |
---|
| 31 | << " seconds, which is " << t0.elapsed_max()/3600.0 << " hours\n"; |
---|
| 32 | |
---|
| 33 | cout << "\nverify progress_display(0) doesn't divide by zero" << endl; |
---|
| 34 | progress_display zero( 0 ); // verify 0 doesn't divide by zero |
---|
| 35 | ++zero; |
---|
| 36 | |
---|
| 37 | long loops; |
---|
| 38 | timer loop_timer; |
---|
| 39 | const double time = 1.0; |
---|
| 40 | |
---|
| 41 | cout << "\ndetermine " << time << " second iteration count" << endl; |
---|
| 42 | for ( loops = 0; loops < LONG_MAX |
---|
| 43 | && loop_timer.elapsed() < time; ++loops ) {} |
---|
| 44 | cout << loops << " iterations"<< endl; |
---|
| 45 | |
---|
| 46 | long i; |
---|
| 47 | bool time_waster; // defeat [some] optimizers by storing result here |
---|
| 48 | |
---|
| 49 | progress_timer pt; |
---|
| 50 | timer t1; |
---|
| 51 | timer t4; |
---|
| 52 | timer t5; |
---|
| 53 | |
---|
| 54 | cout << "\nburn about " << time << " seconds" << endl; |
---|
| 55 | progress_display pd( loops ); |
---|
| 56 | for ( i = loops; i--; ) |
---|
| 57 | { time_waster = loop_timer.elapsed() < time; ++pd; } |
---|
| 58 | |
---|
| 59 | timer t2( t1 ); |
---|
| 60 | timer t3; |
---|
| 61 | t4 = t3; |
---|
| 62 | t5.restart(); |
---|
| 63 | |
---|
| 64 | cout << "\nburn about " << time << " seconds again" << endl; |
---|
| 65 | pd.restart( loops ); |
---|
| 66 | for ( i = loops; i--; ) |
---|
| 67 | { time_waster = loop_timer.elapsed() < time; ++pd; } |
---|
| 68 | |
---|
| 69 | if ( time_waster ) cout << ' '; // using time_waster quiets compiler warnings |
---|
| 70 | progress_display pd2( 50, cout, "\nLead string 1 ", "Lead string 2 ", "Lead string 3 " ); |
---|
| 71 | for ( ; pd2.count() < 50; ++pd2 ) {} |
---|
| 72 | |
---|
| 73 | cout << "\nt1 elapsed: " << t1.elapsed() << '\n'; |
---|
| 74 | cout << "t2 elapsed: " << t2.elapsed() << '\n'; |
---|
| 75 | cout << "t3 elapsed: " << t3.elapsed() << '\n'; |
---|
| 76 | cout << "t4 elapsed: " << t4.elapsed() << '\n'; |
---|
| 77 | cout << "t5 elapsed: " << t5.elapsed() << '\n'; |
---|
| 78 | cout << "t1 and t2 should report the same times (very approximately " |
---|
| 79 | << 2*time << " seconds).\n"; |
---|
| 80 | cout << "t3, t4 and t5 should report about the same times,\n"; |
---|
| 81 | cout << "and these should be about half the t1 and t2 times.\n"; |
---|
| 82 | cout << "The following elapsed time should be slightly greater than t1." |
---|
| 83 | << endl; |
---|
| 84 | return 0; |
---|
| 85 | } // main |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | |
---|