(C++) std::difftime
January 10, 2018 · View on GitHub
(C++) std::difftime
std::difftime is an STL function to obtain the time difference in ticks between two std::clock_t structures.
std::difftime 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