C++ Standard Library C++ STL Library

C++ <ctime> - clock_t Type



The C++ <ctime> clock_t type is an alias of a fundamental arithmetic type capable of representing clock tick counts. Clock ticks are units of time of a constant but system-specific length, as those returned by clock() function.

In the <ctime> header file, it is defined as follows:

typedef /* unspecified */ clock_t;              

Example:

The example below shows the usage of clock_t type.

#include <iostream>
#include <ctime>
using namespace std;
 
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
  cout<<"Time taken = "<<
        (finish - start)<<" ticks (" <<
        1000.0 * (finish - start)/CLOCKS_PER_SEC
        <<" milliseconds)";   
  return 0;
}

The output of the above code will be:

Time taken = 5591147 ticks (5591.15 milliseconds)

❮ C++ <ctime> Library