SE250:lab-1:tsen009

From Marks Wiki
Jump to navigation Jump to search

Task

The task of the lab was to experiment and determine by using a C programme, how long it takes to execute a "Simple" addition operation (in C).

Methods

What is a simple addition?

most people interpret a simple addition as,

x = x + 1;

given that x was defined before this line of code.

but in reality, this is a simple addition and also writing to a variable, and reading from a variable. therefore i diclair that a simple addition to be simply,

1 + 1;


Basic Method (Method #1)

Libraries

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

main

void main(){
 double addition = 0;
 clock_t start = clock( ); 
 for( int counter = 0; counter < 100000000; counter++ ){
   addition = addition + 1;
 }
clock_t finish = clock( ); 
 printf( "%ld\n", finish - start );
}

More Accurate Method (Method #2)

Libraries

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

main

void main(){
 clock_t start = clock( ); 
 for( int counter = 0; counter < 100000000; counter++ ){
   1 + 1;
 }
clock_t finish = clock( ); 
 printf( "%ld\n", finish - start );
}

Results

Following are time taken for 100000000 additions in milliseconds, in both methods used...

Basic Method (Method #1)

844     836     820     820     821     867     836     820     828     828


More Accurate Method (Method #2)

226     219     219     210     211     211     227     211     211     211

Conclusion

From the experiment, we can determmine roughly it takes ~210-220Ms to do simple additions 100000000. it was clear that a simple addition compaired with a counter would make a very great difrence (counter having to use variables and saving to variables). we have to also keep in mind that the "for loop" also contains a counter. and loop it self takes time. therefore the experiment is only a rough estimate (assuming that the for loop do not take any time to execute). but in reality, the for loop will take much longer to execute than the addition it self(it was shown that a counter takes much longer to execute than a simple addition).

So as far as for the task, it is not possible to accurately determine how fast C would perform a simple addition. it will be mainly dependent on the type variables you use, how the code is written, programmes running that may affect the speed(background/operating system/anti virus/network/file sharing), and mainly the performance of the computer (speed of the cpu/ram/harddrive, and since Uni user data is network stored, the network speed, and the network busy'ness at the time). its too many variables that need to be kept constant to get an accurate answer for this question, and as a student, i am not given enough resources nor time to accurately follow through and find a valid answer for the task.

Future Improvements

  • Try diffrent operating systems
  • Find the time taken for the "For loop" by it self
  • Try diffrent computers, diffrent labs, networks etc.
  • Compair the results against a benchmark testing software for the same computer