Reputation:
I am working on a program in which I want to know the time elapsed between two calls down to the ms.
I was reading the windows documentation on SYSTEMTIME structure (minwinbase.h), QueryUnbiasedInterruptTime function (realtimeapiset.h), Windows time (windows.h) and it sounds like QueryUnbiasedInterruptTime is the way to go, however just curious if there is an established best method for this sort of requirement.
Additionally, is there a convenient way to compare something like a SYSTEMTIME time-object, or would the fastest / easier way just be to break down the object into hours, minutes, seconds, ms, and subtract one from the other?
Upvotes: 1
Views: 286
Reputation: 2415
You don't need anything windows specific to calculate this. What is important is that you need to use a steady clock instead of high_resolution_clock or system_clock (see this) to understand why.
#include <chrono>
#include <iostream>
int main()
{
auto t1 = std::chrono::steady_clock::now();
// someFunctionThatTakesTime()
auto t2 = std::chrono::steady_clock::now();
std::cout<<std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count();
}
To calculate the time period between two days, you use the code in the following answer (the code calculates the days between two dates but you could cast to a different time period if you liked):
Number of days between two dates C++
Upvotes: 1