SE250:lab-1:jpar277

From Marks Wiki
Jump to navigation Jump to search

Hi all!

Well first I thought I'd start by discussing the code I used. The lab handout did state that we might not have done any coding in the last 3 months and that would be very correct in my case! lol So I had to go a review a few bits of code from last years 131 course (thank god I had my usb with me)

Then I needed some assistance with the clock function. I turned to the help menu for this rather than asking the tutors. And I came across a very interesting piece which described exactly what we were doing!

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vccrt/html/3e1853dd-498f-49ba-b06a-f2315f20904e.htm

^^^ url must be inserted in the help menu


The code I ended up using was ::::

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

int main(void) {
	long input = 4000000;
	long i = 0;
	double  duration;

	clock_t start, finish;

	start = clock();
	while(i < input) 
		i++;
	finish = clock();

	duration = (double)(finish - start) / CLOCKS_PER_SEC;

	printf("Time taken to add 1 up to %d is %f seconds\n", input, duration);

	return 0;
}

I started at an input of 1000000 (1million) and worked my way up to 10000000 (10million) in intervals of 100000 (100thousand). As you'd expect the seconds increased with each extra addition of 100000, however my results were only accurate to the nearest millisecond so the increase in time wasn't very exact. I got an increase in time by 1 millisecond every 4-6 increase in 100000.

Something interesting I noticed was that when the number of additions was doubled, the time was longer than double!

1000000 = 0.002s

2000000 = 0.005s !!

and the pattern continued higher up additions.

You may have noticed how my code example contains longs, I tried int only to find I get the same results. Short couldn't be carried out as the number of additions was too little, I couldn't go beyond 2304. I had issues with float and double, it just wouldn't work :S


Here are my full results that I recorded ::::

1000000 = 0.002s

1100000 = 0.003s

1200000 = 0.003s

1300000 = 0.003s

1400000 = 0.003s

1500000 = 0.004s

1600000 = 0.004s

1700000 = 0.004s

1800000 = 0.004s

1900000 = 0.004s

2000000 = 0.005s

2100000 = 0.005s

2200000 = 0.005s

2300000 = 0.005s

2400000 = 0.005s

2500000 = 0.005s

2600000 = 0.005s

2700000 = 0.005s

2800000 = 0.006s

2900000 = 0.006s

3000000 = 0.007s

3100000 = 0.007s

3200000 = 0.007s

3300000 = 0.007s

3400000 = 0.008s

3500000 = 0.008s

3600000 = 0.008s

3700000 = 0.008s

3800000 = 0.008s

3900000 = 0.008s

4000000 = 0.009s

4500000 = 0.010s

5000000 = 0.011s

5500000 = 0.012s

6000000 = 0.013s

6500000 = 0.014s

7000000 = 0.016s

7500000 = 0.017s

8000000 = 0.018s

8500000 = 0.019s

9000000 = 0.020s

9500000 = 0.021s

9600000 = 0.021s

9700000 = 0.021s

9800000 = 0.022s

9900000 = 0.022s

10000000 = 0.022s

It took some time remembering bits of code but in the end I think I got some standard results.