(C++) std::clock
January 17, 2018 ยท View on GitHub
std::clock is an STL time function to obtain the number of ticks the program has been running.
std::clock 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