/* since.c
 *
 * $Id: since.c,v 1.3 2000/12/11 16:15:16 alderman Exp $
 *
 * Call since_init() once at the beginning.  Subsequent calls
 * to since() return the number of ms since the last call or
 * the since_init().
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <values.h>
#include <sys/time.h>
#include <math.h>
#include "timer.h"

static struct timespec now_tv;
static long last;
double mean_since = 0.0, var_since = 0.0;
int ctr = 0;

int init_since(void) {
	clock_gettime(CLOCK_REALTIME, &now_tv);
	last = tv_to_msec(&now_tv);
        ctr = 0;
        mean_since = var_since = 0.0;
	return(1);
}

int now_since(void) {
        long now;
        clock_gettime(CLOCK_REALTIME, &now_tv);
        now = tv_to_msec(&now_tv);
        last = now;
        return(now);
}

int since(void) {
	long now;
        int time_since;
	clock_gettime(CLOCK_REALTIME, &now_tv);
	now = tv_to_msec(&now_tv);
	time_since = (int)(now - last);
	last = now;
        mean_since += time_since;
        var_since += time_since * time_since;
        ++ctr;
        printf("Since %d: %d\n", ctr, time_since);
	return(time_since);
}

int end_since(void) {
	mean_since = mean_since / ctr;
	var_since = var_since / ctr - mean_since*mean_since;
	printf("Mean: %f, Variance: %f, Coefficient: %f\n",
               mean_since, var_since, sqrt(var_since) / mean_since);
        return(1);
}


