SE250:lab-1:rwan064

From Marks Wiki
Revision as of 05:18, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (25 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

Timing the addition operation (+) in C.

Need to time the addition operation for the following data types:

  • int
  • long
  • short
  • float
  • double

Methods

The first method that came to mind was to make a function that does addition operations a large number of times (but less than the limit for that data range) and then time how long it takes to execute that function.

Assumptions:

  • Time taken to run and exit from a function is negligible
  • Time taken to declare and compare variables is also negligible

Timing the functions

The macro used to time the functions. The macro assumes variables named "t1", "t2" and "time" are already declared in the main function of types "clock_t", "clock_t" and "double" respectively.

#define TEST(fname, limit) \
	t1 = clock(); \
	fname(); \
	t2 = clock(); \
	printf( #fname ", Time elapsed: %f\n", (double) (t1 - t2) / (double) CLOCKS_PER_SEC ); \
	time = ((double) (t1 - t2) / (double) CLOCKS_PER_SEC) / limit; \
	printf( "Time for + operation: %f\n", time );

The limits for the loops used to test each data type.

#define INT_LIMIT 2147483647
#define LONG_LIMIT 2147483647
#define SHORT_LIMIT 32767

// For float and double the pre-defined macros in "float.h" were used.

The actual functions that are timed and does the addition operations. The addition operation in the for loop function is done in the incrementing section of the loop and in the while loop function it is done inside the loop.

void test1( void ); // INT
void test2( void ); // INT
void test3( void ); // LONG
void test4( void ); // LONG
void test5( void ); // SHORT
void test6( void ); // SHORT
void test7( void ); // FLOAT
void test8( void ); // FLOAT
void test9( void ); // DOUBLE
void test10( void ); // DOUBLE


void test1( void )
{
	int i;
	for ( i = 0; i < INT_LIMIT; i++ ) {
	}
}

void test2( void )
{
	int i = 0;
	while ( i < INT_LIMIT ) {
		i++;
	}
}

// ... similar for other data types. Each type has two tests.
// One with a for loop and another with a while loop.

The main function showing how the tests are run.

int main( int argc, char *argv[] )
{
	clock_t t1, t2;
	
	double time = 0.0;
	
	TEST( test1, INT_LIMIT );
	TEST( test2, INT_LIMIT );
	puts( "\n" );
	TEST( test3, LONG_LIMIT );
	TEST( test4, LONG_LIMIT );
	puts( "\n" );
	TEST( test5, SHORT_LIMIT );
	TEST( test6, SHORT_LIMIT );
	puts( "\n" );
	TEST( test7, FLT_MAX );
	TEST( test8, FLT_MAX );
	puts( "\n" );
	TEST( test9, DOUBLE_LIMIT );
	TEST( test10, DOUBLE_LIMIT );
	
	return 0;
}

Results

Results from different compilers and environments

Windows Vista: cygwin

test1, Time elapsed: 4294963.375000
test2, Time elapsed: 4294963.421000
test3, Time elapsed: 4294963.436000
test4, Time elapsed: 4294963.437000
test5, Time elapsed: 0.000000
test6, Time elapsed: 0.000000
test7, Time elapsed: 0.000000
test8, Time elapsed: 0.000000
test9, Time elapsed: 0.000000
test10, Time elapsed: 0.000000

Linux server

test1, Time elapsed: -34.890000
test2, Time elapsed: -64.460000
test3, Time elapsed: -34.930000
test4, Time elapsed: -64.070000
test5, Time elapsed: 0.000000
test6, Time elapsed: 0.000000
test7, Time elapsed: 0.000000
test8, Time elapsed: 0.000000
test9, Time elapsed: -0.010000
test10, Time elapsed: 0.000000

Windows XP: Visual Studio 2005

Compiled using the command line compiler.

test1, Time elapsed: -6.390000
Time for + operation: -0.000000
test2, Time elapsed: -6.297000
Time for + operation: -0.000000


test3, Time elapsed: -6.313000
Time for + operation: -0.000000
test4, Time elapsed: -6.296000
Time for + operation: -0.000000


test5, Time elapsed: 0.000000
Time for + operation: 0.000000
test6, Time elapsed: 0.000000
Time for + operation: 0.000000

After test6 the program kept running for about 5 minutes without any output so I terminated the process. I think it takes too long because I am either doing too many calculations or there is a condition that never evaluates to false.

Conclusions

When using cygwin on Windows Vista (in GCL) I got proper (positive) values for the time taken to do the addition operation. But when I tried it using the Linux server from the same machine I got negetive values. Finally I tried a third time on the Engineering Lab using Windows XP and the command line Visual Studio 2005 compiler and also got negetive values.

I have no idea why I get negetive values since the "clock" function returns the number of clock ticks elapsed since the program was run. So it is obvious that calling the "clock" function in a later time would give a higher value. So subtracting the earlier value from the later one should give a positive number.

Overall my approach was the obvious way of timing something. But if I had gone into more depth I could have gotten a better way of doing it.

Problems:

  • I think that neglecting the time taken for doing the comparison operation was a bad idea since the comparison operation is also done the same amount of times as the addition operation. So it might affect my final results significantly.

Improvements:

  • I think a better way of doing it would have been to time the execution of the function itself WITHOUT the addition operation so then I can subtract that result from the one I get when I time the function WITH the addition operation. Since I use the loop incrementation part in the "for" loop as the addition operation I need to add another addition operation inside the for loop if I use this new method.