/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <stdlib.h>

#include "high_res_timer.h"

static void test(const char *fnm, int expect, int blksiz)
{
    FILE *fp;
    char *buf;
    TIMECOUNT_T t1, t2;
    int n, bufsiz, i;
    t1 = GET_TIMECOUNT;
    buf = malloc(blksiz);
    fp = fopen(fnm, "r", "rop=rah", "mbc=127", "mbf=127");
    n = 0;
    while((bufsiz = fread(buf, 1, blksiz, fp)) > 0)
    {
        for(i = 0; i < bufsiz; i++)
        {
            if(buf[i] == '\n') n++;
        }
    }
    if(n != expect)
    {
        printf("%d lines\n", n);
    }
    free(buf);
    fclose(fp);
    t2 = GET_TIMECOUNT;
    printf("C fread (rop=rah mbc=127 mbf=127) (block size %d) : %d ms\n", blksiz, (int)((t2 - t1) * 1000 / UNITS_PER_SECOND));
}

int main(int argc, char *argv[])
{
    int blksiz[] = { 512, 5120, 51200, 512000 };
    int i, j;
    for(j = 0; j < 4; j++) 
    {
        for(i = 0; i < 10; i++)
        {
            test(argv[1], atoi(argv[2]), blksiz[j]);
        }
    }
    return 0;
}
