// ---------------------------------------------------------------------------- // clockTest.cpp // Written by: // Last Modified: // // Program with two timing examples // 1. frequency of primes // 2. looping calling exponential function // // ---------------------------------------------------------------------------- #include // clock_t, clock, CLOCKS_PER_SEC #include // sqrt #include // cout // ---------------------------------------------------------------------------- // Prototypes - global function declarations // ---------------------------------------------------------------------------- clock_t freqySetup(long n); int frequencyOfPrimes(long n); clock_t LoopingTime(long numIterations); using namespace std; struct DataPoints { clock_t time; long n; }; // ---------------------------------------------------------------------------- // main - it all begins here // ---------------------------------------------------------------------------- int main () { cout << "This is a timing test program.\n"; cout << "The numbers used here are _small_ so time variations will be _tiny_\n"; cout << "The lesson is to see that runtimes do indeed vary.\n"; cout << "Run this program 5 times and you may notice the variations.\n"; cout << "Compare to same program running on different machine may be more useful.\n"; cout << "Or on same machine but with more programs running in the background.\n"; cout << endl; long k = 1; DataPoints freqyData[10000]; // pick big array size... just in case we really want a lot of data DataPoints loopyData[10000]; int fIdx = 0; int lIdx = 0; // init all data for (int y = 0; y < 10000; y++) { freqyData[y].time = 0; freqyData[y].n = -1; loopyData[y].time = 0; loopyData[y].n = -1; } // for (long i = 100000; i <= 25000000; i+=300000) // use for larger data set for (long i = 10000; i <= 3000000; i+=300000) { freqyData[fIdx].n = i; freqyData[fIdx].time = freqySetup(i); ++fIdx; ++k; if (k % 3 == 0) { loopyData[lIdx].n = 2*i; loopyData[lIdx].time = LoopingTime(2*i); ++lIdx; k = 1; } } cout << "\n======================================================================\n\n"; // Print out summary data cout << "Freqy Data\n"; for (long g = 0; g < fIdx; g++) { cout << "n = " << freqyData[g].n << " time = \t" << freqyData[g].time << " clicks, "; cout << (freqyData[g].time / (double)CLOCKS_PER_SEC) << " seconds\n"; } cout << "Loopy Data\n"; for (long h = 0; h < lIdx; h++) { cout << "n = " << loopyData[h].n << " time = \t" << loopyData[h].time << " clicks, "; cout << (loopyData[h].time / (double)CLOCKS_PER_SEC) << " seconds\n"; } return 0; } // ---------------------------------------------------------------------------- // freqySetup // This function is used just to put a timing wrapper around // calling frequencyOfPrimes // Returns the click difference endTime - startTime // ---------------------------------------------------------------------------- clock_t freqySetup(long n) { clock_t t; int f; t = clock(); cout <<"Frequency of Primes, n = " << n << endl; f = frequencyOfPrimes(n); cout << "The number of primes <= " << n << " is: " << f << endl; t = clock() - t; cout << " Time = " << t << " clicks = " << (t / (double)CLOCKS_PER_SEC) << " seconds\n"; return t; } // ---------------------------------------------------------------------------- // frequency of primes // Counts the number of primes less than n, returns the result // Expect n > 0, if not use -n, return -1 if zero sent // ---------------------------------------------------------------------------- int frequencyOfPrimes (long n) { if (n < 0) { n = -n; } long q = 0; long i,j; int freq = n-1; for (i=2; i<=n; ++i) { for (j=sqrt(i); j>1; --j) { if (i % j==0) { --freq; break; // hack to exit the inner for-loop early // a good programmer would re-write the loop logic // not use a cheap hack =) } } ++q; if (q % 50000 == 0) { cout << "."; q = 0; } } cout << endl; return freq; } // ---------------------------------------------------------------------------- // LoopingTime // numIterations = maximum number value to loop to // exp(i) will be called each iteration of the for-loop // ---------------------------------------------------------------------------- clock_t LoopingTime(long numIterations) { cout << "\nFor-loop exp test... n = " << numIterations << endl; clock_t start = clock(); for (long i = 0; i < numIterations; ++i) { exp(log((double)i)); if (i % 500000 == 0) { cout << "++"; } } cout << endl; clock_t finish = clock(); clock_t t = finish - start; cout << " Time = " << t << " clicks ="; cout << ( t / (double)CLOCKS_PER_SEC) << " seconds.\n"; return t; } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // end of file