
/*
 * Sander van Veen (UvA ID: 6167969)
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/times.h>
#include <limits.h>
#include "interval.h"
#include <math.h>

void consume_time()
{
    int i = 0, max = 1e6;

    double x = 0, s, e;

    srand(0);

    while( i++ < max )
    {
        x += rand();

        s = sqrt(x);
        e = pow(10, log10(x)/2);
    }
}

/*
 *
 */

int main (int argc, char **argv)
{
    interval id = newInterval();

    // Benchmark the duration of a few system calls using timeInterval.

    double wct = 0,
           ust = 0,
           syt = 0;

    int i = 0, max = 1e2;

    while( i++ < max )
    {
        consume_time();

        (void) timeInterval(id, &wct, &ust, &syt);

        printf("Task took %.3f sec (us: %.3f, sy: %.3f)\n", wct, ust, syt);
    }

    // Benchmark the duration of one million timeInterval calls.

    i = 0;

    double tmp_wc = 0,
           tmp_us = 0,
           tmp_sy = 0;

    interval max_id = newInterval();
    id = newInterval();
    max = 1e6;

    while( i++ < max )
    {
        (void) timeInterval(id, &tmp_wc, &tmp_us, &tmp_sy);
    }

    if( timeInterval(max_id, &wct, &ust, &syt) )
        perror("timeInterval() returned a non-zero error code.");

    fprintf(stderr, "%dx timeInterval took %.3f sec (us: %.3f, sy: %.3f)\n",
            max, wct, ust, syt);

    return (EXIT_SUCCESS);
}
