当我从此页面运行代码时 high_precision_timer ,我知道我的系统只支持微秒精度。
根据文档,
cout << chrono::high_resolution_clock::period::den << endl;
Note, that there isn’t a guarantee how many the ticks per seconds it has, only that it’s the highest available. Hence, the first thing we do is to get the precision, by printing how many many times a second the clock ticks. My system provides 1000000 ticks per second, which is a microsecond precision.
我也得到完全相同的值 1000000 ticks per second 。这意味着我的系统也支持微秒精度。
每次我运行任何程序时,我总是得到值 xyz 微秒和 xyz000 纳秒。我认为我的系统不支持 nanosec 可能是上述原因。
有没有办法让我的系统支持纳秒?
最佳答案
这不是答案。我无法在评论中打印长消息。 我只是测试你的例子。 而我的系统输出结果是: 计时::高分辨率_时钟::周期::den = 1000000000。 我的系统每秒提供 1000000000 个刻度,这是纳秒级的精度。 不是 1000000(微秒)。 您的系统每秒提供 1000000 个滴答声,这是一个微秒的精度。 所以,我不知道如何帮助你。对不起。
#include <iostream>
#include <chrono>
using namespace std;
int main()
{
cout << chrono::high_resolution_clock::period::den << endl;
auto start_time = chrono::high_resolution_clock::now();
int temp;
for (int i = 0; i< 242000000; i++)
temp+=temp;
auto end_time = chrono::high_resolution_clock::now();
cout <<"sec = "<<chrono::duration_cast<chrono::seconds>(end_time - start_time).count() << ":"<<std::endl;
cout <<"micro = "<<chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << ":"<<std::endl;
cout <<"nano = "<<chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count() << ":"<<std::endl;
return 0;
}
关于c++ - 如何让我的系统支持纳秒精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22503544/