This blog is under construction

Saturday 5 May 2012

clock example in C

Header file:
    time.h

Synopsis:
     clock_t clock(void);

Description:
     It returns the processor time used by the program at the time of execution(return value is clock ticks).  clock()/CLOCKS_PER_SEC gives the time in seconds and it returns -1 on failure.


clock function C example:


  #include<stdio.h>
  #include<time.h>
  int main() {
        clock_t clk_val;
        int i;
        while(clock() < 5 * CLOCKS_PER_SEC / 100){}
        clk_val = clock();
        printf("Clocks per second: %ld\n", CLOCKS_PER_SEC);
        printf("Processor time used from the beginning "
                       "of program execution: %ld\n", clk_val);
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/cpgms/time$ ./a.out
  Clocks per second: 1000000
  Processor time used from the beginning of program execution: 50000




No comments:

Post a Comment