What is the best, most accurate timer in C++?

前端 未结 4 2032
暗喜
暗喜 2020-12-06 11:50

What is the best, most accurate timer in C++?

4条回答
  •  醉酒成梦
    2020-12-06 12:30

    In C++11 you can portably get to the highest resolution timer with:

    #include 
    #include 
    #include "chrono_io"
    
    int main()
    {
        typedef std::chrono::high_resolution_clock Clock;
        auto t1 = Clock::now();
        auto t2 = Clock::now();
        std::cout << t2-t1 << '\n';
    }
    

    Example output:

    74 nanoseconds
    

    "chrono_io" is an extension to ease I/O issues with these new types and is freely available here.

    There is also an implementation of available in boost (might still be on tip-of-trunk, not sure it has been released).

提交回复
热议问题