(C++) std::clock_t

January 17, 2018 ยท View on GitHub

std::clock_t is an STL struct for storing time.

std::clock_t is defined in the header file ctime.h.

#include <cstdlib>
#include <ctime>
#include <iostream>

int main()
{
  const std::clock_t begin = std::clock();
  for (int i=0; i!=10000000; ++i) std::rand();
  const std::clock_t end = std::clock();
  const double n_seconds = std::difftime(end,begin) / CLOCKS_PER_SEC;
  std::cout << "Elapsed time: " << n_seconds << " seconds\n";
}

Screen output (elapsed time might differ):

Elapsed time: 0.16 seconds