SE250:lab-1:gfun006

From Marks Wiki
Jump to navigation Jump to search

Lab 1

Problems

- Tried to use the EMAC programme...but did not understand at all what was going on, so switched back to Visual Studio which I found easier to use.

- Before I even started I was trying to remember how C worked, it took a lot of trial and error and staring at friend's computers to figure that out.

- I had NO idea how the clock() function worked until I Google'd it and asked some questions from friends and lecturer.

- Trying to remember simple ENGGEN131 stuff, like proper syntax. That included { } brackets and int main(). I was horrified at my memory loss.

- Was confused whether to use a while or a for loop, in the end settled with a for loop.

My code

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

int x;
int a;
long b;
short c;
float d;
double e;
double time_elapsed; 

int main()
{
clock_t starttiming;
clock_t endtiming; 

starttiming = clock();
for (x = 0; x < 1000000000; x++) {

a = a + a;
}

endtiming = clock();
time_elapsed = ((double) (endtiming - starttiming)) / CLOCKS_PER_SEC;
printf("Time elapsed to execute an int addition command is %lf seconds\n", time_elapsed);  

starttiming = clock();
for (x = 0; x < 1000000000; x++) { 

b = b + b;
} 

endtiming = clock();
time_elapsed = ((double) (endtiming - starttiming)) / CLOCKS_PER_SEC;
printf("Time elapsed to execute an long addition command is %lf seconds\n", time_elapsed); 

  • Repeat the above code from starttiming to the printf line for each main data type (i.e. just change b to c, then to d, then to e).


Explanation & Example

What I did in the above code was for each main data type, I made it do 1 billion additions, and then print the time it took for the processor to calculate all the additions. I was going to write a code to calculate each main data type more than once, and take the average. I would of had to use another loop. But I ran out of time...so yeah :)

An example of one of my code running are as followed:

- Time elapsed to execute an int addition command is: 3.649000 seconds

- Time elapsed to execute an long addition command is: 3.122000 seconds

- Time elapsed to execute an short addition command is: 3.030900 seconds

- Time elapsed to execute an float addition command is: 4.826000 seconds

- Time elapsed to execute an double addition command is: 4.746000 seconds