SE250:lab-1:rthu009
Lab1 rthu009
This lab is pretty easy but it requires having knowledge of using the time.h function. I learnt to use this function by looking through the help content that given in Visual C++ program. I found the information very use full and manage to apply the information successfully creating this time calculation program.
This program uses a simple while loop to add numbers from 0 to 1000000000 one by one. E.g. (0+1=1, 1+1=2 etc up to 1000000000) the number 1000000000 was used to obtain a reasonable time values from the clock signals. I have tried calculating the values up to 100 or 1000 but the computer calculates these values fast which is less than a second long.
Running the program: Step1
Code:
#include <stdio.h> #include <time.h> . int main(void) { . int i=0; clock_t start, finish; double duration; . start = clock(); . while(i<=1000000000){ . i=i+1; } finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf("Time taken to add 0 to 1000000000 by one is: "); printf( "%2.1f seconds\n", duration ); . . }
Program output
Time taken to add 0 to 1000000000 by one is: 2.4 seconds
Running the same program for the Double
When I run this program converting the “i” incrementing value from “int” to “double” I found that the time taken to process the calculation was longer. This is because the double value uses far more characters in its calculations than int’s rounded whole number figures.
Time taken to add 0 to 1000000000 by one is: 9.5 seconds
This shows significant increase in time.