C++ Standard Library C++ STL Library

C++ <ctime> - CLOCKS_PER_SEC macro



The C++ <ctime> CLOCKS_PER_SEC macro expands to an expression of type clock_t equal to the number of clock ticks per second, as returned by clock() function. Dividing a count of clock ticks by this expression yields the number of seconds.

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

#define CLOCKS_PER_SEC /* implementation defined */             

Example:

The example below shows the usage of CLOCKS_PER_SEC macro.

#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