I've been reading up about this, and the numbers don't add up. On my operating system (Windows), I can check the resolution of the system clock like this:
LARGE_INTEGER largeInt; // Basically a struct/union containing a long long
QueryPerformanceFrequency(&largeInt);
// largeInt now holds the frequency of my system's clock, it's 3903987
// This ends up being a resolution of about 256 nanoseconds
All is good, but now I want to use std::chrono to check the same details. According to cppreference.com and a popular answer on this site, the period of a std::chrono clock is a compile-time ratio consisting of a numerator and denominator which specifies how many seconds there are between ticks.
cppreference.com:
period: a std::ratio representing the tick period(i.e.the number of seconds per tick)
And from the a stack overflow answer:
The minimum representable duration is high_resolution_clock::period::num / high_resolution_clock::period::den seconds. You can print it like this:
std::cout <<
(double)std::chrono::high_resolution_clock::period::num /
std::chrono::high_resolution_clock::period::den;
So I tried:
// On Windows the result is typedeffed as intmax_t, which is a typedef for a long long
intmax_t numerator = std::chrono::high_resolution_clock::period::num;
intmax_t denominator = std::chrono::high_resolution_clock::period::den;
// numerator is 1 and denominator is one billion, which would suggest a
// tick period of one nanosecond?
According to what's been explained my system's clock is capable of one nanosecond resolution? The OS doesn't seem to think so. I've also tried some other tools that support that my system's clock frequency is around 3903987, and so I don't see how it could manage a resolution finer than that.
I'm not sure whether the frequency changes with CPU power settings / boost mode or not, though high_resolution_clock::is_steady results as true. I restarted my computer twice and got frequency counters of 3903994 and 3903991, so each time booting up this number seems to change. I guess this wouldn't be ideal to calculate the clock at compile time, as opposed to the start of running the program.
So is there way to get the actual resolution/frequency of your system clock with std::chrono?