Does steady_clock have only 10ms resolution on win

2019-08-06 01:39发布

问题:

I have suprising observation that steady_clock gives poor 10ms resolution when measuring durations. I compile for windows under cygwin. Is it sad true or am I doing sth wrong?

auto start = std::chrono::steady_clock::now();
/*...*/
auto end = std::chrono::steady_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>
                                                   (end - start).count();

Result is 10000,20000 etc.

回答1:

The resolution of std::steady_clock is implementation dependent, and you shouldn't rely on a precise minimum duration. It varies across platforms/compiler implementations.

From http://cppreference.com:

Class std::chrono::steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward. This clock is not related to wall clock time, and is best suitable for measuring intervals.

Related: Difference between std::system_clock and std::steady_clock?

If you don't care about monotonicity (i.e., you don't care if someone changes the wall clock while your program is running), you're probably better off with a std::high_resolution_clock. (the latter is still implementation-dependent)