I'm trying to compute the amount of time an algorithm runs in my program, and display that amount in differents meausures such as seconds, miliseconds, microseconds...
This is how I've been approaching it:
auto start = high_resolution_clock::now();
myFunction();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<seconds>(stop - start);
const auto hrs = duration_cast<hours>(duration);
const auto mins = duration_cast<minutes>(duration - hrs);
const auto secs = duration_cast<seconds>(duration - hrs - mins);
const auto ms = duration_cast<milliseconds>(duration - hrs - mins - secs);
std::cout << "Time needed: "<<hrs.count()<<" hours "<<mins.count()<<" mins "<<secs.count()<<" secs " <<ms.count()<<" milisecs "
What I intend it to do (invented example):
Time needed: 0 hours 1 mins 30 seconds 2400miliseconds
BUt for some reasons it prints all 0s (even miliseconds) when times are <1secs, and when times are 1secs or more, it does not prints the exact time either, it does something like
Time needed: 0 hours 0 mins 1 seconds 1000miliseconds
it just print a whole seconds and then it converts it to the others meausures, but the actual time that I took the function to works is not exactly 1 second, how can I get that? (if theres better ways to do that other than std::chrono I'll be interested too)