1

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?

  • 7
    I would start with [`std::chrono::steady_clock`](https://en.cppreference.com/w/cpp/chrono/steady_clock) and see if that produced sufficient resolution. – user4581301 Jun 06 '22 at 20:19
  • Possible duplicate of https://stackoverflow.com/questions/31905157/how-to-print-the-execution-time-of-my-code – Galik Jun 06 '22 at 20:20
  • IMHO it is a duplicated of the downvoted question https://stackoverflow.com/questions/13384904/how-can-i-get-a-clock-time-accurate-to-1ms-in-windows The comment there *This isn't any more accurate than `GetLocalTime()`, and will be considerably less accurate if Windows adjusts its real time clock to match reality (which it does periodically using NTP). It is more precise however.* And https://stackoverflow.com/questions/60823110/accurate-sleep-wait-windows-os-1ms-accuracy – 273K Jun 06 '22 at 20:20
  • I generally use this answer: https://stackoverflow.com/a/31905437/3807729 – Galik Jun 06 '22 at 20:23
  • 1
    Windows API for measurement are explained here: https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps . But first try the `steady_clock` solution and see if it's enough. – Federico Jun 06 '22 at 20:25
  • 1
    QueryUnbiasedInterruptTime requires a minimum of Windows 7, for compatibility with earlier systems (and also for higher resolution) you can use QueryPerformanceFrequency/QueryPerformanceCounter. – Jonathan Potter Jun 06 '22 at 20:26

1 Answers1

1

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++

Hisham Hijjawi
  • 1,803
  • 2
  • 17
  • 27
  • 1
    Stick to `std::steady_clock`. [A note on `std::high_resolution_clock` from one of the people most responsible for creating it](https://stackoverflow.com/a/37440647/4581301). TL;DR version: He says "My bad." – user4581301 Jun 06 '22 at 23:22