SE250:lab-1:jhor053

From Marks Wiki
Jump to navigation Jump to search

Lab 1 Softeng250


Jhor053


My code:


/*Addition.c Jason Horrocks 4700266 
* 
*A simple C program to consider the measurement of Addition 
* 
*It goes in a for loop milions of time doing addition but 
*is flawed in that the time is recorded for the for loop 
*as well and hence can only be used to show relativity 
*/ 

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

int main(){ 
   int i; //for loop counter 
   double type = 0; //this is the variable that is changing types 
   double start, end; //recording start and end clock time (clocks) 
   double difference; //difference in seconds 

   //starting clocks measurement 
   start = clock(); 

   //starting for loop for doing addition repeatedly 
   for (i = 0; i < 100000000; i++){ 
      type= type + 1; 
   } 

   //stoping clock 
   end = clock(); 

   //work out difference in 'clocks' then convert to seconds 
   difference = ((end - start) / CLOCKS_PER_SEC ); 

   //showing result of how long to do the multiple additions 
   printf("%f seconds\n", difference); 

   return 0; 
} 



It is simple to show the relativity of how long it takes for the different types (int, short, long, float, double) but hard to show the exact time for each addition let alone the exact time for a billion of them.


Here are the averages (5 times repeated) for each type for a billion calculations*:


Type

Time (seconds)


int

3.375


short

3.360


long

3.390


double

4.828


float

4.844


  • Done on a Intel Core2Duo and WinXp sp2


Conclusion:


Here we can see that the more simple types are quicker as less bits involved simply. But to answer the question how long it would take to execute the + operation is hard to say as it is too quick and even in large multiples I haven't take into account the loop (which in its self uses the ++ operation doing addition itself). I guess if I had more time I could rewrite the code to see how long an equivalent loop takes or resurrect my old 386 with a base of 6mhz (or press the turbo button to get 40mhz :S ) to get a better result or try it on a different architecture on my Linux partition or even on my tablet which has a Transmeta Crusoe variable chip. But I digress the simple fact of the matter is that + in C is very quick!