简体   繁体   English

同时获取steady_clock和system_clock

[英]Get steady_clock and system_clock at the same time

My Code looks like: 我的代码如下:

// Next 2 lines should be at the same time
auto nowMonotonic = std::chrono::steady_clock::now();
auto nowSystem = std::chrono::system_clock::now();

// Do some calc with nowMonotonic and nowSystem

This can happen: 这可能发生:

auto nowMonotonic = std::chrono::steady_clock::now();
// Some other thread/process interrupts this thread for an 
// unspecific amount of time...
auto nowSystem = std::chrono::system_clock::now();

// Do some calc with nowMonotonic and nowSystem

I'm working on a (non-realtime) linux system. 我正在研究(非实时)Linux系统。 I'm doing some calculations based on the current steady_clock and the current system_clock . 我正在根据当前的steady_clock和当前的system_clock进行一些计算。 The result is better than less the jitter between the reading of the two clock is. 结果优于两个时钟读数之间的抖动。

What is the best way to read the two clocks at (nearly) the same time? 在(几乎)同时读取两个时钟的最佳方法是什么?

Is there a special system call to do this? 是否有特殊的系统调用来执行此操作? Is there a possibility to get the difference of these two clocks in a single system call? 是否有可能在单个系统调用中获得这两个时钟的差异?

There is no way to do this perfectly, and there is not even a best way. 没有办法完美地做到这一点,甚至没有最好的方法。 And the way you have shown is one of the good ways. 你展示的方式是一种好方法。 If you are willing to trade some run time you can gain some better accuracy in a statistical sense by calling now() more than once on each clock and averaging the results. 如果你愿意交易一些运行时间,你可以通过在每个时钟上多次调用now()并对结果取平均值来获得更好的统计意义。

For example: 例如:

std::pair<std::chrono::steady_clock::time_point,
          std::chrono::system_clock::time_point>
combined_now()
{
    using namespace std::chrono;
    auto u0 = system_clock::now();
    auto t0 = steady_clock::now();
    auto t1 = steady_clock::now();
    auto u1 = system_clock::now();
    return {t0 + (t1-t0)/2, u0 + (u1-u0)/2};
}

This is not necessarily better than what you have. 这不一定比你拥有的更好。 But it is another tool in the toolbox to be aware of. 但它是工具箱中另一个需要注意的工具。 As always, use tests with your specific use case to decide what is best for you. 与往常一样,使用特定用例的测试来确定最适合您的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 stable_clock 与 system_clock 之间的区别? - Difference between steady_clock vs system_clock? std::system_clock 和 std::steady_clock 之间的区别? - Difference between std::system_clock and std::steady_clock? 在C ++实现中std :: chrono :: system_clock vs std :: chrono :: steady_clock的精度是多少? - Precision of std::chrono::system_clock vs std::chrono::steady_clock across C++ implementations? 不匹配的类型 'std::chrono::_V2::steady_clock' 和 'std::chrono::_V2::system_clock' - mismatched types 'std::chrono::_V2::steady_clock' and 'std::chrono::_V2::system_clock' 我怎么知道我的 `std::chrono::high_resolution_clock` 对应于 `steady_clock` 或 `system_clock` 的别名,或其他? - How can I know my `std::chrono::high_resolution_clock` correspond to the alias of `steady_clock` or `system_clock`, or other? 在整个系统中使用 std::chrono::steady_clock 时间是否正确? - Is it correct to use std::chrono::steady_clock time across the system? 将 stable_clock::time_point 转换为 time_t - Converting steady_clock::time_point to time_t 将 stable_clock::time_point (C++) 转换为 System::DateTime (C++/CLI) - Convert steady_clock::time_point (C++) to System::DateTime (C++/CLI) VS11稳定,稳定吗? - VS11 is steady_clock, steady? 持久性 std::chrono time_point<steady_clock></steady_clock> - Persistent std::chrono time_point<steady_clock>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM