SE250:lab-1:kkan048

From Marks Wiki
Jump to navigation Jump to search

got trouble to login Linix. Used window MVS to develop program.

http://www.cplusplus.com/reference/clibrary/ctime/clock.html

Some info about function "clock".

CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).

clock() return the current clock ticks but it is not a value of time.

clock_t is variable defined in <ctime>.


int main() {

       clock_t start_clock_t;
       clock_t end_clock_t;
       int n;
       int a;
       clock_t time_elapsed_t;
       start_clock_t=clock();
       for (n=0; n<100000000; n++)
       {
       }
       end_clock_t=clock();
       time_elapsed_t =(end_clock_t - start_clock_t);
       printf ("%ld",time_elapsed_t);
       return 0;

}

this is the code to test the clock ticks in the addition. it is about 220.

When we add an addition in the for lop.

 for (n=0; n<100000000; n++)
       {
       a++
       }

clock ticks is not much different.

When we change the variable to double.

the clock ticks increase 4 times.

The mean time usage in the for loops is comparing n and the end loop condition.

i want the addition become a mean time usage in the for loop. I try to decrease the comparing times.

#include <stdio.h>
#include <time.h>
int main()
{
       clock_t start_clock_t;
       clock_t end_clock_t;
       int n;
       int a;
       clock_t time_elapsed_t;
       start_clock_t=clock();
       a=0;
       for (n=0; n<10000000; n++)
       {
       a++;
       a++;
       a++;
       a++;
       a++;
       a++;
       a++;
       a++;
       a++;
       a++;

       }
       end_clock_t=clock();
       time_elapsed_t =(end_clock_t - start_clock_t);
       printf ("%ld",time_elapsed_t);
       return 0;

}

the clock ticks decrease to about 188. In second version,5 times more addition command had been done.

i guess the best way to test the time of addition is copy and paste 10000000 times "a++" in the main.c . So pure machine code of addition can run for test. And get the clock ticks number.

The for loops include the compare command which makes the time measurement difficult. I think i can't get answer using for loops