Home >Backend Development >C++ >Why Does `std::chrono::high_resolution_clock`'s Precision Differ from Measured Values?

Why Does `std::chrono::high_resolution_clock`'s Precision Differ from Measured Values?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-09 06:14:02378browse

Why Does `std::chrono::high_resolution_clock`'s Precision Differ from Measured Values?

Why does the accuracy of std::chrono::high_resolution_clock not match the measured value?

Problem:

In the following test program, the program tries to measure the execution time of std::cout, but the measurement results obtained are not consistent and are always 1 or 2 microseconds. This is because the precision of std::chrono::high_resolution_clock does not seem to be 100 nanoseconds, or there is a problem with the code that measures std::cout execution time.

#include <iostream>
#include <chrono>

using std::chrono::nanoseconds;
using std::chrono::duration_cast;

int main()
{
    std::cout << "Resolution (nano) = " << (double) std::chrono::high_resolution_clock::period::num / 
                                          std::chrono::high_resolution_clock::period::den * 
                                          1000 * 1000 * 1000 << std::endl;

    auto t1 = std::chrono::high_resolution_clock::now();
    std::cout << "How many nanoseconds does std::cout take?" << std::endl;
    auto t2 = std::chrono::high_resolution_clock::now();

    auto diff = t2 - t1;
    nanoseconds ns = duration_cast<nanoseconds>(diff);

    std::cout << "std::cout takes " << ns.count() << " nanoseconds" 
              << std::endl;
}

Answer:

In Visual Studio 2012, std::chrono::high_resolution_clock is defined as system_clock, which results in poor accuracy (approx. 1 ms). For greater accuracy, the following custom high-res clock can be used:

HighResClock.h

struct HighResClock
{
    typedef long long                              rep;
    typedef std::nano                              period;
    typedef std::chrono::duration<rep, period>     duration;
    typedef std::chrono::time_point<HighResClock>  time_point;
    static const bool is_steady = true;

    static time_point now();
};

HighResClock.cpp

namespace
{
    const long long g_Frequency = []() -> long long
    {
        LARGE_INTEGER frequency;
        QueryPerformanceFrequency(&amp;frequency);
        return frequency.QuadPart;
    }();
}

HighResClock::time_point HighResClock::now()
{
    LARGE_INTEGER count;
    QueryPerformanceCounter(&amp;count);
    return time_point(duration(count.QuadPart * static_cast<rep>(period::den) / g_Frequency));
}

This clock can be used instead of std::chrono::high_resolution_clock for high-precision time measurement.

The above is the detailed content of Why Does `std::chrono::high_resolution_clock`'s Precision Differ from Measured Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn