SE250:lab-1:dsan039

From Marks Wiki
Jump to navigation Jump to search

Outline

The purpose of this lab was to determine how long it takes to calculate a simple addition of an int, long, short, float or double variable to its own type. (int + int, etc.) The problems encountered included mainly basic syntax errors giving unreasonable results - mainly due to my forgetting syntax and having to google the clock function to understand how to use it. The code I've used takes an average of 50 trials measuring the length of time taken to calculate and addition 100 million times. This means it is very slow and hardly worth running, and even more useless owing to the lack of accuracy. I had to ask for help from my peers on developing a method of recording the software.

Errors include ignoring the fact that the for loop i used has a counting variable which increases along with the actual variable being measured. this would definitely increase processing time. (Not taking into account the overhead). The results (which are v. small values) are without comparison to other machines or OS's and so there is no way to validate them.

improvements could include being more specific with the requirements or outcomes to be achieved within the lab.

results

The number of seconds taken to calculate an integer addition is: 0.000000003471600 The number of seconds taken to calculate a long integer addition is: 0.000000002497600

it takes some time before the results come - the code could be improved by lessening the 50 trials to about 20 and/or using only approx. 1 million and not 100 million additions.

Code

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

int main(void)
{
/* this code is meant to test the speed of the c language when adding different number values*/

int sum = 0;
int integer = 0;
long long_number = 0;
short short_number = 0;
float float_number = 0;
double double_number = 0;

int counter = 0;
clock_t start = 0;
clock_t time_elapsed = 0;
double summation = 0;
 
while (counter != 50) {

	 start = clock();

		 for (integer=1; integer<100000001; integer++){
			 sum++;
	 	}
 
	 time_elapsed = clock()- start;
 
	 summation += (double)time_elapsed;
 
	 counter++;
}

summation = (summation / 50.0) / CLOCKS_PER_SEC;
printf("The number of seconds taken to calculate an integer addition is: %.15f\n", summation / 100000000.0); 
 
counter = 0.0;
(clock_t)start = 0.0;
(clock_t)time_elapsed = 0.0;
summation = 0;
  
while (counter != 50) {
  
	 start = clock();

		 for (long_number=1; long_number<100000001; long_number++){
		 	sum++;
		 }

	 time_elapsed = clock()- start;

	 summation += (double)time_elapsed;

	 counter++;
 }

summation = (summation / 50.0) / CLOCKS_PER_SEC;
printf("The number of seconds taken to calculate a long integer addition is: %.15f\n", summation / 100000000.0);

counter = 0.0;
(clock_t)start = 0.0;
(clock_t)time_elapsed = 0.0;
summation = 0;

while (counter != 50) {

	 start = clock();

	 	for (long_number=1; short_number<100000001; short_number++){
		 	sum++;
		 }

	 time_elapsed = clock()- start;

	 summation += (double)time_elapsed;

	 counter++;
 }
 
summation = (summation / 50.0) / CLOCKS_PER_SEC;
printf("The number of seconds taken to calculate a short integer addition is: %.15f\n", summation / 100000000.0);
 
counter = 0.0;
(clock_t)start = 0.0;
(clock_t)time_elapsed = 0.0;
summation = 0;
 
while (counter != 50) {
 
	 start = clock();

		 for (long_number=1; float_number<100000001; float_number++){
		 	sum++;
		 }
 
	 time_elapsed = clock()- start;
 
	 summation += (double)time_elapsed;
 
	 counter++;
}
 
summation = (summation / 50.0) / CLOCKS_PER_SEC;
printf("The number of seconds taken to calculate a floating point addition is: %.15f\n", summation / 100000000.0);
 
counter = 0.0;
(clock_t)start = 0.0;
(clock_t)time_elapsed = 0.0;
summation = 0;
 
while (counter != 50) {
 
	 start = clock();
 
		 for (long_number=1; double_number<100000001; double_number++){
		 	sum++;
		 }

	 time_elapsed = clock()- start;
 
	 summation += (double)time_elapsed;
 
	 counter++;
}
 
summation = (summation / 50.0) / CLOCKS_PER_SEC;
printf("The number of seconds taken to calculate a double type addition is: %.15f\n", summation / 100000000.0);
 
}