/* A couple of handy functions for timing c code
   adapted from A Book on C - p405
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAXSTRING 100

typedef struct {
	clock_t save_clock;
	time_t save_time;
} time_keeper;

static time_keeper tk;

void start_time(void)
{
	tk.save_clock = clock();
	tk.save_time  = time(NULL);
}

void prn_time(void)
{
	char s1[MAXSTRING], s2[MAXSTRING];
	int field_width, n1, n2;
	double clocks_per_second = (double) CLOCKS_PER_SEC,
	       user_time, real_time;

	user_time = (clock() - tk.save_clock) / clocks_per_second;
	real_time = difftime(time(NULL), tk.save_time);
	
	n1 = sprintf(s1, "%.1f", user_time);
	n2 = sprintf(s1, "%.1f", real_time);
	field_width = (n1 > n2) ? n1 : n2;
	printf("%s%*.1f%s\n%s%*.1f%s\n",
	   "User time: ", field_width, user_time, " seconds",
	   "Real time: ", field_width, user_time, " seconds");
}
