SE250:lab-1:ssre005

From Marks Wiki
Jump to navigation Jump to search

After reading the task sheet I had no idea what to do. Looking at David Olsen's screen reminded me somewhat of how the C language works so I knew where to start. I Asked tutor about the clock() function and found that clock() returns the value of the time at a certain point in the code so to find the time taken to do a certain task, the time at the start and the end of the code must be taken and the difference between the two must be found. This difference is returned in 'clocks' where there are 1000 clocks in 1 second.


I then came up with the following code according to what the tutor suggested:

#include<time.h>
int main (void)
{
    long t, t1, totaltime;
    int i = 0;

    t = clock();

    while i < 1000000000000000000000000;
    i = i + 1;
    end;

    t1 = clock();

    totaltime = (t1 - t)/1000000000000000000000000;
    printf(%l, "totaltime");
}

I was reminded that "end" isn't used in C and that instead of writing 1000000000000000000000000, I could use a variable at the start of the code so that changing this value would be easy. Along with this, a line with "#include <stdio.h>" was needed at the start.

I tried to figure out how to compile using emacs and followed the instructions for emacs setup on SE250:using the lab wiki site. I Compiled and successfully produced a value for the number of clocks taken for the code to compile.

I then realised that the time taken for the loop itself was being taken into account (rather than the time taken for the additions alone, which is what is required of the task) so I came up with the idea of putting the t and t1 variables inside the while loop along the lines of:

    while (i < addNumber){
	t = clock();

	i = i + 1;

	t1 = clock();
	
    }

Then, a problem with the code taking a lot of time to compile (too long to wait for) arose. I asked the tutor and was told that to produce an output using this method, an enormous amount of time would be needed as through each loop of the section of code:

    while (i < addNumber){
	t = clock();
	i = i + 1;
	t1 = clock();
    }

the clock() function is being called twice. As the clock() function takes a significant amount of time to load, doing this millions of times over becomes a problem. The tutor suggested comparing the time it would take for a for loop to run by itself with a for loop along with an addition to run.

I decided to try out the following code:

    t = clock();
    for( i = 0; i < addNumber; i++ ){
    f = f + 1;
    }
	
    t1 = clock();

    addLoopTime =(t1 - t);


    t = clock();
    for( i = 0; i < addNumber; i++ ){
    }
	
    t1 = clock();

    forLoopTime =(t1 - t);

totalTime = addLoopTime – forLoopTime;

This calculates the time taken for the loop with an addition in it to run as well as the time taken for a loop with nothing in it to run and finds the difference. This difference should be the time taken for solely the additions to run. My Final solution is printed below. The 'power' variable is used to write the final solution in standard form for seconds for each individual addition.

#include <time.h>
#include <stdio.h>
int main (void)
{
    int f = 0;
    long t, t1, totalTime;
    long addLoopTime, forLoopTime = 0;
    long i, power = 0;
    long addNumber = 10000000;
    long x = addNumber;

    while (x > 1){
	x = x/10;
	power = power + 1;
    }
   
    t = clock();
    for( i = 0; i < addNumber; i++ ){
    f = f + 1;
    }
	
    t1 = clock();

    addLoopTime =(t1 - t);


    t = clock();
    for( i = 0; i < addNumber; i++ ){
    }
	
    t1 = clock();

    forLoopTime =(t1 - t);


    totalTime = addLoopTime - forLoopTime;
   
    power = power+3;
    
    printf("%ld X 10^ %ld seconds\n", totalTime, power);
    return 0;
}

And the output produced by this was: -16 X 10^ 10 seconds. This suggests that the time taken to execute a for loop with an addition is less than the time taken to execute a for loop that does nothing which does not make much sense.