分辨率与刻度频率

时间:2014-11-06 09:10:53

标签: c++ time

我的理解是

std::chrono::high_resolution_clock::period::num / std::chrono::high_resolution_clock::period::den;

给出滴答频率,但我如何确定时钟的分辨率? (在Windows下,此比率返回1e-007

例如,在high_resolution_clock下,typedefsystem_clock,每15分钟精度为100μs,而boost::chrono high_resolution_clocktypedef steady_clock,我如何确定其解决方案?

1 个答案:

答案 0 :(得分:1)

解决此类问题的最佳方法是测试。此信息无法通过var result = Database.Client.Context.CheckRegister .Where(...) .GroupBy(cr => cr.CheckNumber, // ResultSelector: take every key (which we call checkNumber) // and all CheckRegisters with this CheckNumber // to make one new object: (checkNumber, checkRegistersWithThisCheckNumber) => new { CheckNumber = checkNumber, // for Employees: Select property Employee, and convert them to a List Employees = checkRegistersWithThisCheckNumber .Select(checkRegister => checkRegister.Employee) .ToList(), // Amounts: select the Amounts and Sum Amount = checkRegistersWithThisCheckNumber .Select(checkRegister => checkRegister.Amount) .Sum(), }); API获得,并且通常没有记录。

测试可能会从时钟中以预先分配的数组收集大量时间戳,然后对该收集进行某种统计分析。例如,下面是一个测试,它查找连续<chrono>调用之间的平均,最小和最大报告持续时间。

Clock::now()

使用"date/date.h"标头只是为了更轻松地打印持续时间,而不必手动发现其单位。您可以通过自行设置持续时间来修改此测试以使其不使用"date/date.h"

我用以下命令对此进行了编译:

#include "date/date.h"
#include <iostream>
#include <vector>

template <class Clock>
void
test()
{
    auto warm_up = Clock::now();
    (void)warm_up;
    std::vector<typename Clock::time_point> v(100'000);
    for (auto& t : v)
        t = Clock::now();
    auto m = Clock::duration::max();
    auto M = Clock::duration::zero();
    for (auto i = 1; i < v.size(); ++i)
    {
        auto delta = v[i] - v[i-1];
        if (m > delta)
            m = delta;
        if (M < delta)
            M = delta;
    }
    using date::operator<<;
    std::cout << "Average delta between calls is "
              << (v.back() - v.front())/(double)(v.size()-1) << '\n';
    std::cout << "Minimum delta between calls is " << m << '\n';
    std::cout << "Maximum delta between calls is " << M << '\n';
}

int
main()
{
    test<std::chrono::system_clock>();
}

在macOS上,运行3次并得到:

clang++ -std=c++17 test.cpp -I../date/include -O3 -Wall

那是给Average delta between calls is 0.049440µs Minimum delta between calls is 0µs Maximum delta between calls is 40µs Average delta between calls is 0.048790µs Minimum delta between calls is 0µs Maximum delta between calls is 45µs Average delta between calls is 0.047210µs Minimum delta between calls is 0µs Maximum delta between calls is 38µs 的。更改为system_clock,我得到了:

steady_clock

我不必为Average delta between calls is 56.912339ns Minimum delta between calls is 43ns Maximum delta between calls is 26973ns Average delta between calls is 63.223292ns Minimum delta between calls is 45ns Maximum delta between calls is 79589ns Average delta between calls is 55.261153ns Minimum delta between calls is 44ns Maximum delta between calls is 21786ns 运行测试,因为此时钟是该平台上high_resolution_clock的类型别名。