gpt4 book ai didi

c++ - 如何从 steady_clock 返回耗时作为原始数据类型 (double)

转载 作者:行者123 更新时间:2023-11-27 22:48:54 26 4
gpt4 key购买 nike

首先,让我说一下,我昨天才开始使用这个库,所以我对它的理解还很基础。我正在 try catch 我正在创建的视觉处理程序的 FPS,并使用 chrono 库将其输出到屏幕。在我的例子中,我需要将启动 steady_clock 后耗时转换为 double(或其他一些我可以像 double 一样对待的数字类型定义)。我查看了引用文档并尝试使用 duration_casttime_point_cast 函数,但这两个似乎都不是我想要的。

我的问题是;有什么方法可以简单地将时钟当前状态的数值(以秒为单位)转换为原始数据类型?

如有任何帮助,我们将不胜感激。

最佳答案

像这样:

#include <chrono>
#include <iostream>
#include <thread>

int main()
{
using namespace std::literals;

// measure time now
auto start = std::chrono::system_clock::now();

// wait some time
std::this_thread::sleep_for(1s);

// measure time again
auto end = std::chrono::system_clock::now();

// define a double-precision representation of seconds
using fsecs = std::chrono::duration<double, std::chrono::seconds::period>;

// convert from clock's duration type
auto as_fseconds = std::chrono::duration_cast<fsecs>(end - start);

// display as decimal seconds
std::cout << "duration was " << as_fseconds.count() << "s\n";
}

示例输出:

duration was 1.00006s

关于c++ - 如何从 steady_clock 返回耗时作为原始数据类型 (double),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39812165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com