// Time a simple nested loop program segment. // Input is the size of the problem instance, n // In order to time very short segments the segment is repeated until // the total time is at least minTime. The total time is divided by // the number of repetitions to estimate the time for one segment run. // written 16 jun 98 tom bailey #include using namespace std; #include "timer.h" void task( ) { // any constant time task. // this one just returns. return; } int main( ) { double const minTime = 2.0; long n; cout << "Enter n ( <= 0 to halt ): "; cin >> n; while( n>0 ) { Timer watch; int runs = 0; while( watch.time() < minTime ) { watch.start(); // This is the code segment being timed. for( int i=1; i<=n; ++i ) { task(); } // End of timed code segment. watch.stop(); runs++; } double time = watch.time() / runs; cout << endl << "n = " << n << ", time = ( " << runs << " runs @ " << time << " seconds per run )" << endl; cout << endl << "Enter n ( <= 0 to halt ): "; cin >> n; } return 0; }