Facebook Page Twitter Page LinkedIn Page
× C Standard Library

The C <time.h> clock() function returns the processor time consumed by the program. The returned value is expressed in clock ticks, which are units of time of a constant but system-specific length. Dividing a count of clock ticks by CLOCKS_PER_SEC yields the number of seconds.

The epoch used as reference by clock varies between systems, but it is related to the program execution. To calculate the actual processing time of a program, the value returned by clock shall be compared to a value returned by a previous call to the same function.


clock_t clock ();


No parameter is required.

Return Value

Returns number of clock ticks elapsed since an epoch related to the particular program execution. On failure, the function returns a value of -1.


The below example shows the usage of clock() function.

#include <stdio.h>
#include <time.h>
int main (){
  clock_t start, finish;
  long product;

  start = clock();
  for(int i = 0; i < 100000; i++)
    for(int j = 0; j < 25000; j++) 
      product = i*j;

  finish = clock();

  //calculating the time difference 
  //in ticks and in milliseconds
  printf("Time taken = %ld ticks (%lf milliseconds)",
        (finish - start), 1000.0 * (finish - start)/CLOCKS_PER_SEC);   
  return 0;

The possible output of the above code could be:

Time taken = 6334701 ticks (6334.701000 milliseconds)

❮ C <time.h> Library