// 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 #include using namespace std; #include "timer.h" void task( ) { // any constant time task. // this one calculates some weird trig stuff double x = 0.0; double dx = 0.1234; double sum = 0.0; while( x < 1.0 ) { sum += sin( x ) + cos( x ); x += dx; } return; } int main( ) { double const minTime = 2.0; long n; cout << "Enter n ( <= 0 to halt ): "; cin >> n; while( n>0 ) { Timer watch; long runs = 0; while( watch.time() < minTime ) { watch.start(); // This is the code segment being timed. for( long i=1; i<=n; ++i ) { for( long j=1; j<=n; ++j ) { 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; }