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

void doTiming( char* command, int nIncludes );
void doTest( char* command, char* fileName );
double currentTime();

#define WHERE printf( "%s, %d\n", __FILE__, __LINE__ )

int main( int argc, char** argv )
{
	if( argc >= 3 )
	{
    	int i;
        
        for( i = 2; i < argc; ++i )
			doTiming( argv[ 1 ], atoi( argv[ i ] ) );
	}
	else
	{
		printf( "Usage:\n" );
        printf( "timeincl compiler n1 [ n2 n3 n4 ... ]\n\n" );
        printf( "Where n1, n2, n3 etc. are the number of #includes\n" );
        printf( "to be timed\n" );
	}

	return 0;
}

void doTiming( char* command, int nIncludes )
{
	char internalGuardFileName[ 100 ];
	char externalGuardFileName[ 100 ];
	char noGuardFileName[ 100 ];

	sprintf( internalGuardFileName, "ig%d.c", nIncludes );
	sprintf( externalGuardFileName, "eg%d.c", nIncludes );
	sprintf( noGuardFileName, "ng%d.c", nIncludes );

	doTest( command, externalGuardFileName );
	doTest( command, internalGuardFileName );
	doTest( command, noGuardFileName );
}

void doTest( char* command, char* fileName )
{
	char systemString[ 1000 ];
	double startTime;
	double finishTime;

	strcpy( systemString, command );
	strcat( systemString, "   " );
	strcat( systemString, fileName );
	strcat( systemString, " > temp" );

	startTime = currentTime();
	system( systemString );
	finishTime = currentTime();

	printf( "Time to execute %s : %0.2f\n", systemString, finishTime - startTime );
}

double currentTime()
{
	double result = clock();
	result /= CLOCKS_PER_SEC;

	return result;
}
