I'm quiet experienced in programming, but new to C++. I'm trying to measure the time it takes to run a code. In the future I might write code that can take hours/days to finish itself. Therefore it is important for me to know the limits of the chrono time measurement. Accuracy in milliseconds should be sufficient.
What is the maximum time I can measure?
I have used the following code, please let me know if this can be improved:
#include <chrono>
using namespace std::chrono;
auto start = high_resolution_clock::now();
// calculations here
auto finish = high_resolution_clock::now();
duration<double> elapsed = finish - start; // elapsed time in seconds
cout << elapsed.count();
Here's an informative little HelloWorld:
#include <chrono>
#include <iostream>
int
main()
{
using namespace std::chrono;
using namespace std;
using years = duration<double, ratio_multiply<ratio<86'400>, ratio<146'097, 400>>>;
cout << years{high_resolution_clock::time_point::max() -
high_resolution_clock::now()}.count()
<< " years until overflow\n";
}
I first create a double-based years duration so that the output is easy to read. Then I subtract now()
from the time_point
's max()
, convert that to years and print it out.
For me this just output:
292.256 years until overflow