#include <v_lib.h>

#define	BUFSIZE	(1024*1024)
#define	NB		1

char buf[BUFSIZE];

void small_reads()
{
	vhandle_t f = fileOpenRead("s_data");
	if(f == LIBV_INVALID_HANDLE)
		return;
	while(!fileEof(f))
		fileRead(f,buf,1);
	fileClose(f);
}

void small_writes()
{
	int i;
	vhandle_t f = fileOpenWrite("s_data");
	if(!f)
		return;
	for(i=0;i<BUFSIZE*NB;i++)
		fileWrite(f,buf,1);
	fileClose(f);
}

void big_reads()
{
	vhandle_t f = fileOpenRead("s_data");
	if(f == LIBV_INVALID_HANDLE)
		return;
	while(!fileEof(f))
		fileRead(f,buf,BUFSIZE);
	fileClose(f);
}

void big_writes()
{
	int i;
	vhandle_t f = fileOpenWrite("s_data");
	if(!f)
		return;
	for(i=0;i<NB;i++)
		fileWrite(f,buf,BUFSIZE);
	fileClose(f);
}

void sys_small_reads()
{
	FILE *f = fopen("s_data","rb");
	if(!f)
		return;
	while(!feof(f))
		fgetc(f);
	fclose(f);
}

void sys_small_writes()
{
	int i;
	FILE *f = fopen("s_data","wb");
	if(!f)
		return;
	for(i=0;i<BUFSIZE*NB;i++)
		fputc(0,f);
	fflush(f);
	fclose(f);
}

void sys_big_reads()
{
	FILE *f = fopen("s_data","rb");
	if(!f)
		return;
	while(!feof(f))
		fread(buf,BUFSIZE,1,f);
	fclose(f);
}

void sys_big_writes()
{
	int i;
	FILE *f = fopen("s_data","wb");
	if(!f)
		return;
	for(i=0;i<NB;i++)
		fwrite(buf,BUFSIZE,1,f);
	fflush(f);
	fclose(f);
}

int main()
{
	vec_t tm;
	vhandle_t tmr = timerCreate();
	timerStart(tmr);

	timerDiffTime(tmr);
	small_writes();
	tm = timerDiffTime(tmr);
	printf("small_writes(): %g s\n",tm);

	timerDiffTime(tmr);
	big_writes();
	tm = timerDiffTime(tmr);
	printf("big_writes(): %g s\n",tm);

	timerDiffTime(tmr);
	small_reads();
	tm = timerDiffTime(tmr);
	printf("small_reads(): %g s\n",tm);

	timerDiffTime(tmr);
	big_reads();
	tm = timerDiffTime(tmr);
	printf("big_reads(): %g s\n",tm);

	timerDiffTime(tmr);
	sys_small_writes();
	tm = timerDiffTime(tmr);
	printf("sys_small_writes(): %g s\n",tm);

	timerDiffTime(tmr);
	sys_big_writes();
	tm = timerDiffTime(tmr);
	printf("sys_big_writes(): %g s\n",tm);

	timerDiffTime(tmr);
	sys_small_reads();
	tm = timerDiffTime(tmr);
	printf("sys_small_reads(): %g s\n",tm);

	timerDiffTime(tmr);
	sys_big_reads();
	tm = timerDiffTime(tmr);
	printf("sys_big_reads(): %g s\n",tm);
	
	
	return 0;
}
