SE250:lab-1:hlin079

From Marks Wiki
Jump to navigation Jump to search

i started with an empty project and a new file. i wrote the for loop. but i found that i don't know how to use the clock function for the timing. so i went on internet search for the clock function.then i wrote my code. but i forgot to declared the type for the time 1 and time 2. also i was thinking that the clock funtion will give the time with out me write some code find out the time1 and time2. after the build project i find out that i need to put the clk1=clock();before my for loop so when the pc start the for loop they find out the time at that instant.also i put the clk2=clock();after my for loop to count the time taken to finish the loop. i set my i maxmum to >1000 and i got 0 second. i think i got o sec becouse that my "i" was too small. so i changed my i to > 1000000000,for that i got 3.123 .

my code`\
  1. include <stdio.h>
  2. include <time.h>

int main(){ int i;

clock_t clk1;

clock_t clk2; double time;

clk1=clock(); for (i=0;i<1000000000;i++){ i=i+1;} clk2=clock(); time=(clk2-clk1)/(double) CLOCKS_PER_SEC;

printf("processing %d addition takes %f",i,time); }