[英]Precision of std::chrono::system_clock vs std::chrono::steady_clock across C++ implementations?
The following program: 以下程序:
#include <chrono>
#include <iostream>
#include <vector>
inline uint64_t now() {
return std::chrono::duration_cast
<std::chrono::nanoseconds>
(std::chrono::system_clock::now()
.time_since_epoch())
.count();
}
int main() {
std::vector<uint64_t> v;
for (int i = 0; i < 1000; i++)
v.push_back(now());
for (int i = 0; i < v.size()-1; i++)
std::cout << v[i+1] - v[i] << std::endl;
}
prints numbers in the range of about 250 to 300 on: 打印数量在250到300左右的范围内:
g++ (Ubuntu 8.2.0-7ubuntu1) 8.2.0
with: 有:
Linux 4.18.0-15-generic #16-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux
Meaning std::chrono::system_clock is nanosecond precision on this system (most likely gettimeofday right?). 意思是std :: chrono :: system_clock在这个系统上是纳秒精度(很可能是gettimeofday对吗?)。 I have several questions: 我有几个问题:
Whats the difference on this system between std::chrono::system_clock
and std::chrono::steady_clock
? 这个系统在std::chrono::system_clock
std::chrono::steady_clock
std::chrono::system_clock
和std::chrono::steady_clock
什么? (Yes, I know they are specified differently in the standard, I am taking about this implementation.) (是的,我知道它们在标准中有不同的指定,我正在考虑这个实现。)
Is the answer the same across all libstdc++ targets? 所有libstdc ++目标的答案是否相同?
Is the answer the same across all libc++ targets? 所有libc ++目标的答案是否相同?
Is the answer the same on Windows/MSVC targets? Windows / MSVC目标的答案是否相同?
I'm not sure you are asking the questions you want answered. 我不确定你是否在问你想要回答的问题。 One thing I see is you asking about difference between steady and system clocks, in terms of their precision. 我看到的一件事是你在稳定性和系统时钟方面的问题,就其精度而言。 The second, judging from the snippet alone, is about the performance of the system_clock::now, duration_cast, vector::push_back/vector::insert and (implicit) vector::resize. 第二,从单独的片段判断,是关于system_clock :: now,duration_cast,vector :: push_back / vector :: insert和(implicit)vector :: resize的性能。
I'll try to answer the first of those two, if you don't mind: 如果你不介意,我会尝试回答这两个中的第一个:
So, asking about any particular implementation and hoping their constants will be also used in other implementations -- even for the same vendor -- is not advisable. 因此,询问任何特定的实现并希望它们的常量也将用于其他实现 - 即使对于同一个供应商 - 也是不可取的。 I'd always try to either use clock's::time_point, or its ::duration, or, as a last resort, milliseconds or nanoseconds, depending on what do I measure and how fast the measured thingines can fly. 我总是尝试使用clock的:: time_point,或者:: duration,或者作为最后的手段,毫秒或纳秒,这取决于我测量什么以及测量的东西飞得多快。
And also please note there are system_clock::(to/from)_time_t() functions, which definitely will produce a 1 over 1 value (seconds), even if the system_clock::duration has a finer period. 还请注意有system_clock ::(to / from)_time_t()函数,即使system_clock :: duration具有更精细的周期,它肯定会产生1超过1的值(秒)。
The revised snippet, using steady_clock, its time_point and calling duration_cast as late as possible would be: 使用steady_clock,其time_point和尽可能晚地调用duration_cast的修订片段将是:
#include <chrono>
#include <iostream>
#include <vector>
int main() {
using namespace std::chrono;
using clock = steady_clock;
std::vector<clock::time_point> v;
for (int i = 0; i < 1000; i++)
v.push_back(clock::now());
for (size_t i = 0; i < v.size()-1; i++) {
std::cout
<< duration_cast<nanoseconds>(
v[i+1] - v[i]
).count()
<< "ns\n";
}
}
Edit: Oh, and another thing is there is nothing in the original code that would prove your library uses nano as a period in the system_clock. 编辑:哦,另一件事是原始代码中没有任何内容可以证明你的库在system_clock中使用nano作为句点。 You are doing a duration_cast<nanoseconds> (which uses integer division if it must) and getting the period from that, but with different duration, something like duration_cast<duration<long long, pico>>, you could also get the nonzero somewhere below the lowest 1000. Not likely, but possible never the less. 你正在做一个duration_cast <nanoseconds>(如果必须的话,它使用整数除法)并从中获得周期,但持续时间不同,例如duration_cast <duration <long long,pico >>,你也可以在下面的某个地方获得非零值最低的1000.不太可能,但可能永远不会少。
Edit 2: Sheesh this is complicated. 编辑2: Sheesh这很复杂。 Changed the reason for system_clock being unsteady in the first bullet point. 在第一个项目符号点中更改了system_clock不稳定的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.