/* $Id: timer.c,v 1.2 2008/10/03 15:49:08 hahe Exp hahe $ */
/* Simple time taker by Hartmut Henkel, 2008 */
/* Public Domain */

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

int main(void)
{
    float dtime;
    int c, ut;
    struct timeval stv, stv0, *tv, *tv0;
    struct timezone stz = { 0, 0 }, *tz;

    FILE *f;

    f = fopen("timer.log", "w");

    tz = &stz;

    tv = &stv;
    tv0 = &stv0;

    int i;
    for (i = 0;; i++) {
        c = getchar();
        if (c == -1) {
            fclose(f);
            return 0;
        }
        gettimeofday(tv, tz);
        if (i == 0) {
            tv0->tv_sec = tv->tv_sec;
            tv0->tv_usec = tv->tv_usec;
        }
        dtime = tv->tv_sec - tv0->tv_sec;
        ut = tv->tv_usec - tv0->tv_usec;
        if (ut < 0) {
            ut += 1e6;
            dtime -= 1.0;
        }
        dtime += ut / 1.0e6;

        fprintf(f, "%d %f\n", i, dtime);
        printf("%d %f", i, dtime);
    }
    return 0;
}

