/* 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 <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

#include "high_res_timer.h"

#define OFFSET 0
#define SIZE 1000000000

static void test(const char *fnm)
{
    int fd;
    char *b;
    TIMECOUNT_T t1, t2;
    int n, ix;
    t1 = GET_TIMECOUNT;
    fd = open(fnm, O_RDONLY);
    if(fd == -1)
    {
        printf("Error opening file\n");
        exit(1);
    }
    b = mmap(NULL, SIZE, PROT_READ, MAP_PRIVATE, fd, OFFSET);
    if(b == MAP_FAILED)
    {
        printf("Error mapping file\n");
        exit(1);
    }
    n = 0;
    for(ix = 0; ix < SIZE; ix++)
    {
        if(b[ix] == '\n')
        {
            n++;
        }
    }
    printf("%d lines\n", n);
    munmap(b, SIZE);
    close(fd);
    t2 = GET_TIMECOUNT;
    printf("Map file to memory : %d ms\n", (int)((t2 - t1) * 1000 / UNITS_PER_SECOND));
}

int main(int argc, char *argv[])
{
    int i;
    for(i = 0; i < 3; i++)
    {
        test(argv[1]);
    }
    return 0;
}
