如何从steady_clock返回经过的时间作为原始数据类型(double)
How To Return Elapsed Time From steady_clock as a Primitive Data Type (double)
首先,我要说的是,我昨天才开始使用这个库,所以我对它的理解仍然相当基础。我试图捕捉我正在创建的视觉处理程序的FPS,并使用chrono库将其输出到屏幕。在我的例子中,我需要将启动steady_clock后所花费的时间强制转换为double(或者其他一些可以视为double类型的数值类型定义)。我查看了参考文档,并尝试使用duration_cast和time_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() << "sn";
}
示例输出:
duration was 1.00006s
可以使用duration::count
函数。
例如,您可以获得以毫秒为单位的持续时间,然后将该计数除以1000.0
,得到作为double
的秒数。
相关文章:
- 防止主数据类型C++的隐式转换
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在C++中打印指向不同基元数据类型的指针的内存地址
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何计算数据类型的范围,例如int
- C++中数据类型修饰符的顺序
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 特定数据类型的模板类
- C++:将值赋值给原始数据类型(例如布尔值)是原子操作吗?
- BoostSpirit.Qi-针对原始数据类型进行边界检查
- 用于操作原始内存的正确数据类型
- 测试与C 原始数据类型上同步锁定的需求
- 将原始数据类型传递到C 中的函数
- 将原始数据类型转换为空隙指针类型
- 从数据库中以 blob 数据类型形式存储的原始图像数据中检索 CImage 对象
- 是否可以将Boost uint128_t数据类型存储为原始int类型?
- 如何将原始数据解析为文字类型
- 如何从steady_clock返回经过的时间作为原始数据类型(double)
- 在循环中分配的原始数据类型的次数
- 在c++函数中传递原始数据类型的最佳实践