c++ - 如何让我的系统支持纳秒精度

标签 c++ linux timer performance-testing

当我从此页面运行代码时 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/

相关文章:

c++ - VS 2010 打开相机但只显示灰色窗口

linux - "no space left on device"- 在 ubuntu 机器上运行 docker-compose 时。无助于清理和重启

c - 可移植的获取时间的方式

linux - 在 bash 中打印字符串模式和新行之间的文本

java - main(String[]) 上的 Swing Timer 在指定时间后退出程序

javascript - 更改 NodeJS 定时器回调函数

c++ - 使用 QFile::readAll 函数时如何避免读取 "\n"

c++ - 如何使用 mpi 将文件从一个进程传输到所有其他进程?

c++ - 为什么我的顶点缓冲区对象出现访问冲突错误?

linux - 关于更新 linux-headers