简体   繁体   English

chrono steady_clock没有给出正确的结果?

[英]chrono steady_clock not giving correct result?

I have a single line of code in my app server code which gets me the timestamp value using steady_clock as shown below: 我的app服务器代码中有一行代码,它使用steady_clock获取时间戳值,如下所示:

uint64_t now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();

Now we have two systems machineA which is running Ubuntu 12 (gcc 4.6.3 compiler) and machineB which is running Ubuntu 14 (gcc 4.8.2 compiler) . 现在我们有两个系统machineA运行Ubuntu 12 (gcc 4.6.3 compiler)和machineB运行Ubuntu 14 (gcc 4.8.2 compiler)

Now we compile our app server code using make on another Ubuntu 12 VM (which has 4.7.3 compiler) and then copy the tar file that gets generated to machineA and start our app server. 现在我们在另一个 Ubuntu 12 VM (which has 4.7.3 compiler)上使用make编译我们的app服务器代码,然后将生成的tar文件复制到machineA并启动我们的app服务器。 After the start, the above line of code prints out value like this in machineA: 在开始之后,上面的代码行在machineA中打印出如下值:

1439944652967

Now we also compile our same app server code using make on another Ubuntu 14 VM (which has 4.8.2 compiler) and then copy the tar file that gets generated to machineB and start our app server. 现在我们还使用另一个 Ubuntu 14 VM (which has 4.8.2 compiler)上的make编译相同的应用服务器代码,然后将生成的tar文件复制到machineB并启动我们的app服务器。 After the start, the above line of code prints out value like this in machineB: 在开始之后,上面的代码行在machineB中打印出这样的值:

10011360 

You see the difference right? 你看到差异吧? I am confuse why this is the difference, I am not able to understand this? 我很困惑为什么这是差异,我无法理解这一点? All the code and everything is same. 所有代码和一切都是一样的。 Does anyone have any explanations about this and how can I fix it? 有没有人对此有任何解释,我该如何解决?

If needed, I can try adding some debug code to see what's wrong to figure out this issue. 如果需要,我可以尝试添加一些调试代码,看看弄清楚这个问题有什么不对。

I'm afraid there's been some confusion over what std::steady_clock is. 我担心std::steady_clock会有什么混乱。

time_since_epoch gives the duration since the beginning of the clock, not necessarily the Unix epoch. time_since_epoch给出了从时钟开始以来的持续时间,不一定是Unix纪元。 steady_clock only guarantees to be monotonically increasing. steady_clock只保证单调增加。 This means that steady_clock will always be moving forward and that it will never decrease. 这意味着steady_clock将始终向前移动并且永远不会减少。

There is no guarantee about steady_clock representing anything meaningful. 无法保证steady_clock代表任何有意义的东西。 It can be the duration since the beginning of the program execution, the duration that the computer has been turned on, the duration since the most recent Tuesday, or pretty much anything as long as it continues to move forward. 它可以是自程序执行开始以来的持续时间,计算机打开的持续时间,自最近的星期二以来的持续时间,或者只要它继续向前移动的几乎任何东西。

In other words, steady_clock is not actually all that useful to tell real world time . 换句话说, steady_clock实际上并不是讲真实世界时间的有用信息 It is only useful to measure the passage of time. 它只对测量时间的流逝有用。 It's uses could include any situation in which you have point in time A and point in time B and you're curious about the duration between them: benchmarking, progress estimates, etc. 它的用途可能包括您有时间点A和时间点B的任何情况,并且您对它们之间的持续时间感到好奇:基准测试,进度估算等。

If you're looking for real world time, you should look into std::system_clock , a clock that represents the time of the system (ie the operating system's time). 如果您正在寻找真实的世界时间,您应该查看std::system_clock ,这是一个代表系统时间的时钟(即操作系统的时间)。 It's great for telling time, but it's pretty pretty useless for measuring differentials since it is not guaranteed to be monotonic and is almost certainly not given daylight saving time, users adjusting their clocks, and other events that can alter real world time. 它非常适合讲述时间,但它对于测量差异非常无用,因为它不能保证单调,几乎肯定没有夏令时,用户调整时钟,以及其他可以改变现实世界时间的事件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM