SE250:lab-1:bvic005

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

Hi all!

The first problem i ran into with this lab was that, as the handout suggested, i had not used c for 3-4 months, and i could not remember any syntax. This was easy to solve with a quick google search. The best resources i found where this syntax guide, and this function reference.

With my newly remember knowledge of C, i proceeded to start up TextPad and cgywin, and start programing.

I started off with a simple for loop, with two pre assigned ints in it, being added together and assigned to a third int. I then added timing functionality using the clock function(the aforementioned function reference was very useful for learning how to use this). I ran the for loop a billion times, and the timer showed that it was taking around 8.4 seconds. This equates to 8.5 nano seconds per loop execution, which is quite small. There was a bit of variability in this value, however. The range of values was around 8.4-8.6 seconds.

I then got my program to divide the time by the number of loop runs, and ended up getting 0.000000 seconds for the single loop execution time. This was fixed by specifying the number of decimal places printf was to print(i used 15).

I then added a empty for loop, timed its execution time, and subtracted that from the raw addition loop execution time, before dividing by the number of loop runs. This, in theory, eliminated the time taken for a the actual loop out of the total time, leaving you with the addition operation time. However, when i ran this, i found that the time for the empty loop was very very close to the main loop execution time, resulting in very small addition execution times, and even negative ones, presumably due to the variations in the loop execution times. The results where in the pico second range, and almost all different. Using different variable types(ie, double, float, etc) didn't seem to result in significant differences between them either, and where all just as variable as each other.

It seems like the addition time is basically negligible when compared to the for loop execution time, especially with the variation in execution times observed. What i would like to do to get some meaningful results out of this is to run the empty loop a few hundred times and get an average value for that, and do the same for the non empty loops, and see if a better average result emerges. There was not time in the lab for this, however, but i will try running it at home, and will post my results, if i get anything useful.

This is the code i used for my tests in the lab(it is currently configured for ints):

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

int main() {

	//Declare some variables
	double i;
	double maxRuns = 100000000.0; //number of runs the loops will go through

	//The added variables
	int number1 = 12448;
	int number2 = 15542;
	int number3;

	//The added variables
	//short number1 = 12448;
	//short number2 = 15542;
	//short number3;

	//The added variables
	//long number1 = 12448;
	//long number2 = 15542;
	//long number3;

	//The added variables
	//double number1 = 12448;
	//double number2 = 15542;
	//double number3;

	//The added variables
	//float number1 = 12448;
	//float number2 = 15542;
	//float number3;

	//Variables used for time calculation
	double clockStart, clockFinish, runtime, additionTime;

	double emptyTime; //Records empty loop time



	//Test empty for loop
	printf("Calculate empty for loop run time:\n");



	//Get the clock at the start
	clockStart = clock();

	//Run the loop
	for(i = 0; i < maxRuns; i++) {
	}

	//Get the clock at the finish
	clockFinish = clock();

	//Calculate times
	emptyTime = (clockFinish - clockStart)/CLOCKS_PER_SEC;

	//Print results
	printf("Time in seconds is %f\n", emptyTime);



	//Test for loop, adding ints, assigning to int
	printf("Calculate run time when adding two ints, and assigning to another int:\n");



 	//Get the clock at the start
	clockStart = clock();

	//Run the loop
	for(i = 0; i < maxRuns; i++) {
		number3 = number1 + number2;
	}

	//Get the clock at the finish
	clockFinish = clock();

	//Calculate times
	runtime = ((clockFinish - clockStart)/CLOCKS_PER_SEC) - emptyTime;
	additionTime = runtime/maxRuns;

	//Print results
	printf("Time in seconds is %f\n", runtime);
	printf("Time per Addition is %.15f\n", additionTime);

	return 0;
}

Bvic005 12:48, 4 March 2008 (NZDT)