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

#define CALLOC_FUNC calloc1
#define PAT_FUNC    pat1_getsize
#define NUMITER     2000000
#define PAT1_SZ     11

void *calloc2(size_t nmemb, size_t size);
void *calloc3(size_t nmemb, size_t size);

static size_t pat1_getsize(size_t iter) { return PAT1_SZ; }
static size_t pat2_getsize(size_t iter) { return  9; }
static size_t pat3_getsize(size_t iter)
{
	static size_t arr[] = { 3, 5, 7, 9, 11, 9, 7, 5 };
	return arr[(iter%(sizeof(arr)/sizeof(size_t)))];
}

/*
 * a - b
 */
static long diff_timespec_ns( struct timespec *a, struct timespec *b )
{
        if ( a->tv_nsec < b->tv_nsec ) {
                a->tv_nsec += 1000000000;
                a->tv_sec--;
        }
        return a->tv_nsec - b->tv_nsec + (a->tv_sec - b->tv_sec)*1000000000;
}

int main(int argc, char *argv[])
{
	struct timespec start,end;
	size_t i;
	clock_gettime(CLOCK_REALTIME,&start);

	for( i = 0; i < NUMITER; ++i )
		CALLOC_FUNC(PAT_FUNC(i),sizeof(size_t));

	clock_gettime(CLOCK_REALTIME,&end);
	printf("executed in %ld ns\n",diff_timespec_ns(&end,&start));
	return 0;
}

