module test;

import std.stdio;
import std.conv;
import std.gc;
import std.c.time;

alias int suseconds_t;

struct timeval {
	time_t      tv_sec;     /* seconds */
	suseconds_t tv_usec;    /* microseconds */
}

struct timezone {
	int tz_minuteswest;     /* minutes west of Greenwich */
	int tz_dsttime;         /* type of DST correction */
}

extern (C) int gettimeofday(timeval *tv, timezone *tz);

timeval tv_s, tv_e;

struct node_t
{
	node_t* next;
	ulong   data;
};

int main(char[][] args)
{
	if (args.length < 2)
		return 1;

	ulong n = toUlong(args[1]);

	//std.gc.disable(); "D gc dis" were with this line uncommented
	gettimeofday(&tv_s, null);
	node_t list = node_t(null, -1uL);
	node_t* it = &list;
	for (int i = 0; i < n; ++i)
	{
		it.data = i;
		it.next = new node_t;
		it = it.next;
	}
	it.next = null;
	gettimeofday(&tv_e, null);
	ulong elapsed1 = (tv_e.tv_sec - tv_s.tv_sec) * 1000000uL
				+ (tv_e.tv_usec - tv_s.tv_usec);

	gettimeofday(&tv_s, null);
	it = &list;
	while (it)
	{
		++(it.data);
		it = it.next;
	}
	gettimeofday(&tv_e, null);
	ulong elapsed2 = (tv_e.tv_sec - tv_s.tv_sec) * 1000000uL
				+ (tv_e.tv_usec - tv_s.tv_usec);

	writefln("Fill:  ", elapsed1, " usecs");
	writefln("Inc:   ", elapsed2, " usecs");
	writefln("Total: ", elapsed1 + elapsed2, " usecs");
	return 0;
}


