SE250:lab-1:llay008

From Marks Wiki
Jump to navigation Jump to search

It took some time at the start to familiarise myself with C (i.e. trying to remember which library (#include <stdio.h.>) to call at the start.) I looked at using emacs to write the program but decided against it because I don't know how it works, so I used Visual Studio.

I started with the easy part - doing the addition. I decided to use a while loop. i was initially set to 0, and incremented until it reached 100000000. Each time it looped a simple addition was performed. The value a was initialized as an int value.

while (i < 100000000){
     a = 1 + 2;
     i++;
}

To compare the main data types I used different variables set to the different data types.

Next, I incorporated the clock function. This was difficult because I did not know how it worked and had to look it up. I found the information by using the help on visual studio. The search function was not very straightforward and it took a couple of tries to find the information. The best way to find the information was to use the index and to filter the search parameters for C++. I found the syntax and an example on how to use it which was very useful.

The clock was initialized: clock_t t0, t1. t0 = clock() was written above the while loop and t1 = clock() below. This recorded the time before and after the while loop and stored the times as t0 and t1 respectively.

To calculate the final time I subtracted t1 - t0. This found the time in hundred millionths of a second. I left the value as it was, because otherwise I got a value that was too small and came up as 0.

I noticed that each time the time taken to do the addition was different. This links to what is mentioned in the handout about the complexity of calculating a programs run time. Even though the code has not changed other factors have and this has affected the run time.

Results. The numbers in bold are averages.

 int    long   short  float  double
 241    220    212    210    232
 266    212    217    213    270
 231    212    220    214    249
 224    214    218    256    211
 247    322    224    221    211
 242    236    218    223    235

These results are very similar. The short addition data consistantly took a comparativlely small amount of time, while the float and long data contained outliers (256 and 322), which affected the averages calculated. The int and double data more consistantly contained larger numbers.

Upon reviewing my work I noticed that I did not take into consideration the time taken to do a loop when I wrote the program. However, when I ran the program using just an empty loop, I found that I got results that were similar to the value recorded for the loop and addition.

I also realised that I was doing two additions - the addition of a = 1 + 2 and the addition of i = i + 1, which means that my program would need to be edited to take this (and the time for the loop) before it could be used to accurately measure the time of one addition.