
#include <ctime>
#include <iostream>

using namespace std;

const int NUM_TIMES = 1000000;

double timingRuns(int numRepeats, double fcn(double arg), int argument);
double wasteTime(double i){return i;}

double factTail(double n);
double tfact(double n, double total);
double fact(double n);

int main(){

   double time;

   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(8);

   time = timingRuns(NUM_TIMES, wasteTime, 7);
   cout << "time is: " << time << endl;

   time = timingRuns(NUM_TIMES, fact, 30);
   cout << "fact time is: " << time << endl;
   time = timingRuns(NUM_TIMES, factTail, 30);
   cout << "tail time is: " << time << endl;

   return 0;
}

double timingRuns(int numRepeats, double fcn(double arg), int argument){

   clock_t start,finish;
   double time;

   start = clock();

   for (int i = 0; i < numRepeats; i++)
       fcn(argument);

   finish = clock();
   time = (double(finish)-double(start))/CLOCKS_PER_SEC;

   return time;
}

double fact(double n){
   if (n < 2)
       return n;
   else
       return n * fact(n-1);
}

double factTail(double n){return tfact(n, 1);}

double tfact(double n, double product){
    if (n == 1)
	return product;
    else
	return tfact(n-1, n * product);
}

		
	

