简体   繁体   中英

Precision of chrono::system_clock::now in VS2015

I am seeing a weird behavior when calling chrono::system_clock::now() on VS2015. If you call chrono::system_clock::now() in a loop the time_points generated seem to have a precision of 1 millisec. Here is the code:

using cl = chrono::system_clock;
set<cl::time_point> s;
for( int i=0; i <1000'000;++i) {
    s.emplace(cl::now());
}
for(const auto& x : s) {
    cout << duration_cast<nanoseconds>(x.time_since_epoch()).count() << endl;
}
cout << "size of set " << s.size() << endl;

Here is the tail of the output time stamps (in nanosec):

1,501,268,149,325,000,700
1,501,268,149,326,000,700
1,501,268,149,327,000,700
1,501,268,149,328,000,700
1,501,268,149,329,000,700
1,501,268,149,330,000,700
1,501,268,149,331,000,700
1,501,268,149,332,000,700
1,501,268,149,333,000,700
1,501,268,149,334,000,700
1,501,268,149,335,000,700
1,501,268,149,336,000,700
1,501,268,149,337,000,700
1,501,268,149,338,000,700
1,501,268,149,339,000,700
1,501,268,149,340,000,700
1,501,268,149,341,000,700
1,501,268,149,342,000,700
1,501,268,149,343,000,700
1,501,268,149,344,000,700
size of set 89

As can be seen from the time stamps it seems like the system_clock is generating times with 1 millisec precision. Although windows system_clock is supposed to have a precision of 100 nanosec. Note that for a million messages it only produced 89 distinct time stamps.

If we run the same code with clang we do not see this problem and the precision in that case is 1 microsec. Also using steady_clock on VS2015 the precision is 1 nanosec.

Does any one know why system_clock behaves this way on VS2015 ?

The implementation by Microsoft is like this:

namespace std
{
    namespace chrono
    {
        struct system_clock
        {
            static time_point now() __NOEXCEPT
            {
                return (time_point(duration(_Xtime_get_ticks())));
            }

_Xtime_get_ticks() uses GetSystemTimeAsFileTime() . This function's precision is 1 millisecond.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM