c++ chrono 时间库
1 概述
chrono 是c++11中的时间库 包含计时,时钟等功能。
2 概念
Time Point 时间点
ratio 时间精度,自定义时间颗粒
Duration 时间段
3 精度
template <intmax_t N,intmat_t D=1> class ratio;
其中 N是分子 D 是分母 默认用秒标识时间单位
ratio <60,1> 60/1(秒)分钟 minutes
ratio <1,1> 一秒seconds
ratio <1,1000> 千分之一秒milliseconds
以上是我们生活中常见的时间颗粒。
时间精度或者理解为周期,表示 你所计量时间段的一个粒度,理解为 要把 N 秒 分成 D 份 作为时间粒度。
minutes
seconds
milliseconds
以上是我们生活中常见的时间颗粒。
4 时间段
template<calss Rep, class Period=ratio<1> >
class duration
Rep 一种数值类型 表示 Period 的数量 int float double .
Period 是 ratio 精度类型
表示 这个时间段 的长度 为 精度类型 * Rep
count() 方法返回共过了多少个ratio .类型为上述Rep提供
std::chrono::hours 小时 类似于 typedef duration<Rep,ratio<3600,1>> hours
std::chrono::minutes 分钟
std::chrono::milliseconds 毫秒
std::chrono::microseconds 微秒
std::chrono::nanoseconds 纳秒
以上都是 时间段 的特化类型。
5 chrono 时钟
chrono 提供了三种时钟类型
system_clock、steady_clock、high_resolution_clock
6 计算程序 耗时 的示例用法
auto start = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double,std::milli> tm =end -start ; //千分之一秒 毫秒;两个时间点做减法 得到一个时间段,这个时间段的最小统计时间颗粒为毫秒.
std::cout <<<tm.count() <<"ms"<<std::endl;;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端