[英]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.