std::chrono笔记

news2024/9/25 23:18:26

文章目录

    • 1. radio
      • 原型
      • 作用
      • 示例
    • 2. duration
      • 原型:
      • 作用
      • 示例
    • 3. time_point
      • 原型
      • 作用
      • 示例
    • 4. clocks
      • system_clock
        • 示例
      • steady_clock
        • 示例
      • high_resolution_clock

先说感觉,这个库真恶心,刚接触感觉跟shi一样,特别是那个命名空间,太长了。
就像这样:
在这里插入图片描述

后来感觉设计还挺巧妙,才觉得看起来顺眼一些。

1. radio

原型

template <intmax_t N, intmax_t D = 1> class ratio;

作用

时间换算的比率 / 表示时间精度。
ratio是一个比率的模板(分子/分母)。第一个模板参数是分子,第二个模板参数是分母,也可以只提供第一个,这样分母默认是0。主要用来表示精度、

可以把它当成一个分数,仅有ratio我们是做不了什么的。

示例

	ratio<60, 1> minute;
	ratio<1, 1> second;
	ratio<1, 1000> millisecond;

当然,头文件有预定义的例子,可以参考:

  typedef ratio<1,       1000000000000000000> atto;
  typedef ratio<1,          1000000000000000> femto;
  typedef ratio<1,             1000000000000> pico;
  typedef ratio<1,                1000000000> nano;
  typedef ratio<1,                   1000000> micro;
  typedef ratio<1,                      1000> milli;
  typedef ratio<1,                       100> centi;
  typedef ratio<1,                        10> deci;
  typedef ratio<                       10, 1> deca;
  typedef ratio<                      100, 1> hecto;
  typedef ratio<                     1000, 1> kilo;
  typedef ratio<                  1000000, 1> mega;
  typedef ratio<               1000000000, 1> giga;
  typedef ratio<            1000000000000, 1> tera;
  typedef ratio<         1000000000000000, 1> peta;
  typedef ratio<      1000000000000000000, 1> exa;

2. duration

原型:

template <class Rep, class Period = ratio<1> > class duration;

构造函数(想快点理解话,主要看第4个):

(1)duration() = default;    //默认构造
(2)duration (const duration& dtn);        //(2)(3)拷贝构造
(3)template<class Rep2, class Period2>
   constexpr duration (const duration<Rep2,Period2>& dtn);
(4)template<class Rep2>      //传递一个某类型(int等)的数值,构造一个时间段   
   constexpr explicit duration (const Rep2& n);

作用

表示一段时间。
第一个模板参数Rep可以是int、float、double,第二个模板参数Period用来表示精度。Rep表示Period的数目。

duration模板中的方法count用来返回Period的数量,返回值是Rep类型。

不同类型的duration之间的转换用duration_cast<>,原型如下:

template <class ToDuration, class Rep, class Period>
constexpr ToDuration
duration_cast (const duration<Rep,Period>& dtn);

示例

#include <iostream>
#include <chrono>
#include <ctime>

using namespace std;
using namespace chrono;

int main (void)
{
	using seconds_type = duration<int, ratio<1, 1>>;
	using minutes_type = duration<int, ratio<60, 1>>;
	using hours_type = duration<int, ratio<3600, 1>>;
	using milliseconds_type = duration<int, ratio<1, 1000>>;
	
	seconds_type oneday_seconds (3600 * 24);
	hours_type oneday_hours (24);
	minutes_type oneday_minutes (60 * 24);
	milliseconds_type oneday_milliseconds (3600 * 24 * 1000);
	cout << "一天的秒数: " << oneday_seconds.count() << endl;
	cout << "一天的小时数: " << oneday_hours.count() << endl;
	cout << "一天的分钟数: " << oneday_minutes.count() << endl;
	cout << "一天的毫秒数: " << oneday_milliseconds.count() << endl;
	
	hours_type oneday_hours_from_milliseconds = duration_cast<hours_type> (oneday_milliseconds);
	hours_type oneday_hours_from_seconds = duration_cast<hours_type> (oneday_seconds);
	hours_type oneday_hours_from_miniues = duration_cast<hours_type> (oneday_minutes);
	cout << "转换后>>" << endl;
	cout << "一天的小时数:" << oneday_hours_from_milliseconds.count() << endl;
	cout << "一天的小时数:" << oneday_hours_from_seconds.count() << endl;
	cout << "一天的小时数:" << oneday_hours_from_miniues.count() << endl;
	
	cout << "一天的秒数:" << duration_cast<seconds_type> (oneday_milliseconds).count() << endl;
	cout << "一天的秒数:" << duration_cast<seconds_type> (oneday_minutes).count() << endl;
	cout << "一天的秒数:" << duration_cast<seconds_type> (oneday_hours).count() << endl;
	
	cout << "一天的毫秒数:" << duration_cast<milliseconds_type> (oneday_seconds).count() << endl;
	cout << "一天的毫秒数:" << duration_cast<milliseconds_type> (oneday_minutes).count() << endl;
	cout << "一天的毫秒数:" << duration_cast<milliseconds_type> (oneday_hours).count() << endl;
}

打印结果:

一天的秒数: 86400
一天的小时数: 24
一天的分钟数: 1440
一天的毫秒数: 86400000
转换后>>
一天的小时数:24
一天的小时数:24
一天的小时数:24
一天的秒数:86400
一天的秒数:86400
一天的秒数:86400
一天的毫秒数:86400000
一天的毫秒数:86400000
一天的毫秒数:86400000

也可以用头文件中预定义的:

    /// nanoseconds
    using nanoseconds	= duration<_GLIBCXX_CHRONO_INT64_T, nano>;
    /// microseconds
    using microseconds	= duration<_GLIBCXX_CHRONO_INT64_T, micro>;
    /// milliseconds
    using milliseconds	= duration<_GLIBCXX_CHRONO_INT64_T, milli>;
    /// seconds
    using seconds	= duration<_GLIBCXX_CHRONO_INT64_T>;
    /// minutes
    using minutes	= duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>;
    /// hours
    using hours		= duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>;
   ...
   ...

3. time_point

原型

template <class Clock, class Duration = typename Clock::duration>
  class time_point;

另外看一下system_clock中内容:

    struct system_clock
    {
      typedef chrono::nanoseconds				duration;
      typedef duration::rep					rep;
      typedef duration::period					period;
      typedef chrono::time_point<system_clock, duration> 	time_point;

		...

标准库有三种钟:system_clocksteady_clockhigh_resolution_clock

可以看出system_clock::time_point默认用的钟是system_clockduration是纳秒级别的nanoseconds

同时,为了转换不同的time_point,还提供了time_point_cast

template <class ToDuration, class Clock, class Duration>
time_point<Clock,ToDuration>
time_point_cast (const time_point<Clock, Duration>& tp);

作用

表示时间点,一个time point必须有一个clock计时

示例

  1. 打印
	system_clock::time_point tp_epoch;
	
	time_point<system_clock, seconds> tp_seconds (seconds (1));
	time_point<
	//默认的time_point是纳米级:
	system_clock::time_point tp (tp_seconds);
	
	printf ("system_clock中,一秒对应%d个时钟周期\n",
			 tp.time_since_epoch().count() );
	
	system_clock::time_point
	time_t time = system_clock::to_time_t (tp);
	printf ("tp的打印结果:%s", ctime (&time));

打印结果:

system_clock中,一秒对应1000000000个时钟周期
tp的打印结果:Thu Jan  1 08:00:01 1970
  1. 转换
	using days_type = duration<int, ratio<3600 * 24>>;
	time_point<system_clock, days_type> today
	    = time_point_cast<days_type> (system_clock::now());
	printf ("从epoch到现在的天数: %d", today.time_since_epoch().count());

打印结果:

从epoch到现在的天数: 19415

到这里应该更能体会到,那个ratio<3600 * 24>,表示一个单位,int其实是单位的数量,整体看起来就是一段时间,只不过这段时间的精度是按天来算的,且数目是int类型。

4. clocks

system_clock

system_clock表示当前的系统时钟,系统中运行的所有进程使用now()得到的时间是一致的。
每一个clock类中都有确定的time_point, duration, Rep, Period类型。
操作有:

  • now()
    • 当前时间time_point
  • to_time_t()
    • time_point转换成time_t
  • from_time_t()
    • time_t转换成time_poin

示例

计算时间日期

#include <iostream>
#include <chrono>
#include <ctime>
#include <fmt/format.h>

using namespace std;
using namespace chrono;

int main (void)
{
	using days_type = duration<int, ratio<3600 * 24>>;
	days_type one_day (1);
	
	system_clock::time_point today = system_clock::now();
	system_clock::time_point tomorrow = today + one_day;
	
	time_t time = system_clock::to_time_t (today);
	printf ("今天是:%s", ctime (&time));
	time = system_clock::to_time_t (tomorrow);
	printf ("明天是:%s", ctime (&time));
}

steady_clock

steady_clock 为了表示稳定的时间间隔,后一次调用now()得到的时间总是比前一次的值大(这句话的意思其实是,如果中途修改了系统时间,也不影响now()的结果),每次tick都保证过了稳定的时间间隔。
操作有:

  • now()
    • 获取当前时钟

示例

典型的应用是给算法计时。

#include <iostream>
#include <chrono>
#include <ctime>
#include <fmt/format.h>

using namespace std;
using namespace chrono;

int main (void)
{
	steady_clock::time_point t1 = steady_clock::now();
	cout << "打印1000个 *" << endl;
	
	for (int i = 0; i < 1000; ++i)
		cout << "*";
	cout << endl;
	steady_clock::time_point t2 = steady_clock::now();
	
	duration<double> time = duration_cast<duration<double>> (t2 - t1);
	cout << "花费的秒数:" << time.count() << endl;
}
*************************************...
花费的秒数:9.3816e-05

high_resolution_clock

最后一个时钟,high_resolution_clock 顾名思义,这是系统可用的最高精度的时钟。实际上high_resolution_clock只不过是system_clock或者steady_clocktypedef
操作有:

  • now()
    • 获取当前时钟。

参考博客:

C++11 std::chrono库详解

c++11 chrono全面解析(最高可达纳秒级别的精度)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/376313.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

人工智能高等数学--人工智能需要的数学知识_微积分_线性代数_概率论_最优化---人工智能工作笔记0024

然后我们看一下人工智能中需要的数学知识 数学知识是重要的,对于理解人工智能底层原理来说很重要,但是工作中 工作中一般都不会涉及的自己写算法之类的,只是面试,或者理解底层原理的时候需要 然后看一下人工智能需要哪些数学知识 这里需要微积分 线性代数 概率论 最优化的知识…

狂神说:面向对象(三)——多态

多态// 对象能执行什么方法&#xff0c;主要看对象左边的类型&#xff0c;和右边的没有关系多态&#xff1a;同一方法可以根据发送对象的不同而采用不同的行为方式父类&#xff1a;public class Person {public void run(){System.out.println("Person > run");}}…

跳跃游戏 (贪心/动态规划/dfs)

1.跳跃游戏简单介绍 跳跃游戏是一种典型的算法题目&#xff0c;经常是给定一数组arr[]&#xff0c;从数组的某一位置i出发&#xff0c;根据一定的跳跃规则&#xff0c;比如从i位置能跳arr[i]步&#xff0c;或者小于arr[i]步&#xff0c;或者固定步数&#xff0c;直到到达某一位…

Java 【数据结构OJ题十道】—— 二叉树篇2

文章目录一、二叉树前序遍历二、二叉树层序遍历三、按照之字形打印二叉树四、二叉树中和为某一值的路径(一)五、二叉搜索树与双向链表六、合并二叉树七、二叉树的镜像八、判断是否为二叉搜索树九、判断是否为完全二叉树十、判断是否为平衡二叉树总结提示&#xff1a;本人是正在…

TCP中RTT时延的理解

最近服务器环境部署了tcprtt网络时延监控&#xff0c;发现不同服务器不同节点之间的RTT时延表象非常奇怪&#xff0c;无法准确的判断服务器的网络情况。因此需要弄清楚什么是RTT&#xff0c;以及能否作为服务器网络性能的检测指标。 1、RTT是什么&#xff1f; TCP中的RTT指的是…

倾向得分匹配案例分析

一、倾向得分匹配法说明 倾向得分匹配模型是由Rosenbaum和Rubin在1983年提出的&#xff0c;首次运用在生物医药领域&#xff0c;后来被广泛运用在药物治疗、计量研究、政策实施评价等领域。倾向得分匹配模型主要用来解决非处理因素&#xff08;干扰因素&#xff09;的偏差。 …

为什么硬件性能监控很重要

当今的混合网络环境平衡了分布式网络和现代技术的实施。但它们并不缺少一个核心组件&#xff1a;服务器。保持网络正常运行时间归结为监控和管理导致网络停机的因素。极有可能导致性能异常的此类因素之一是硬件。使用硬件监控器监控网络硬件已成为一项关键需求。 硬件监视器是…

连接金蝶云星空,数据交互轻松搞定!丨三叠云

金蝶云星空 路径 拓展 >> 插件 功能简介 新增插件「金蝶云星空」。 用户可通过配置「金蝶云星空」插件&#xff0c;就可以实时获取「金蝶云星空」的数据&#xff0c;同时支持回填数据至金蝶系统内。 地图视图 路径 表单 >> 表单设计 功能简介 新增「地图视…

prometheus+cadvisor监控docker

官方解释 cAdvisor&#xff08;ContainerAdvisor&#xff09;为容器用户提供了对其运行容器的资源使用和性能特性的了解。它是一个正在运行的守护程序&#xff0c;用于收集、聚合、处理和导出有关正在运行的容器的信息。具体来说&#xff0c;它为每个容器保存资源隔离参数、历史…

活动目录(Active Directory)组策略管理工具

活动目录&#xff08;Active Directory&#xff09;是面向Windows Standard Server、Windows Enterprise Server以及 Windows Datacenter Server的目录服务。&#xff08;Active Directory不能运行在Windows Web Server上&#xff0c;但是可以通过它对运行Windows Web Server的…

虚拟数字人直播带货相比人工有哪些优势?

新经济时代的到来&#xff0c;彻底改变了传统的消费方式。虚拟数字人的出现&#xff0c;标志着新一波的消费升级到来。虚拟数字人直播带货&#xff0c;不仅降低了商家的带货成本&#xff0c;拉近了商家与消费者的距离&#xff0c;也给消费者带来全新的消费方式。 花西子虚拟形象…

华为OD机试模拟题 用 C++ 实现 - 删除最少字符(2023.Q1)

最近更新的博客 【华为OD机试模拟题】用 C++ 实现 - 最多获得的短信条数(2023.Q1)) 文章目录 最近更新的博客使用说明删除最少字符题目输入输出描述示例一输入输出示例二输入输出Code使用说明 参加华为od机试,一定要注意不要完全背诵代码,需要理解之后模仿写出,通过率…

在Redis集群模式下使用pipeline进行批量操作

最近开始又接触到了Redis&#xff0c;之前在工作中使用Redis的时候&#xff0c;由于QPS不高&#xff0c;都是直接get/set搞定了。这次遇到的业务数据量比较大&#xff0c;更新也很频繁&#xff0c;Redis使用是集群模式&#xff0c;所以本文记录下捣鼓出来的如何在集群模式下使用…

动手学深度学习(第二版)学习笔记 第三章

第三章 线性神经网络 代码&#xff1a;d2l-zh/pytorch/chapter_linear-networks 3.1 线性回归 3.1. 线性回归 — 动手学深度学习 2.0.0 documentation 解析解 线性回归的解可以用一个公式简单地表达出来&#xff0c;这类解叫作解析解&#xff08;analytical solution&…

深度学习实战19(进阶版)-SpeakGPT的本地实现部署测试,基于ChatGPT在自己的平台实现SpeakGPT功能

大家好&#xff0c;我是微学AI&#xff0c;今天给大家带来SpeakGPT的本地实现&#xff0c;在自己的网页部署&#xff0c;可随时随地通过语音进行问答&#xff0c;本项目项目是基于ChatGPT的语音版&#xff0c;我称之为SpeakGPT。 ChatGPT最近大火&#xff0c;其实在去年12月份…

「架构」全链路异步模式

总结自尼恩的全链路异步&#xff1a;网关纯异步化网关层的特点&#xff1a;不需要访问业务数据库只做协议转换和流量转发特点是 IO 密集型&#xff0c;特别适合纯异步的架构&#xff0c;可以极大的节省资源。如何进行网关异步化&#xff1f;使用高性能的通信框架Netty&#xff…

CSS3新增的视口单位Vh、Vw单位

定义vw&#xff1a;浏览器可见视口【宽度】的百分比&#xff08;1vw代表视窗【宽度】的1%&#xff09;vh&#xff1a;浏览器可见视口【高度】的百分比&#xff08;1vw代表视窗【高度】的1%&#xff09;vmin&#xff1a;当前 vw 和 vh 较小的一个值。vmax&#xff1a;当前 vw 和…

现在入行软测=49年入国军?三句话,让面试官再掏2K!

还有三五天就步入金三银四&#xff0c;很多软测人吐槽因为疫情&#xff0c;公司都在裁员&#xff0c;别说跳槽涨薪&#xff0c;能保住现在的工作就不错了。但也有那么一批人&#xff0c;凭借自己口才与实力拿到年薪近50W的offer。面试是初见1小时就要相互了解优缺点的过程&…

软考知识笔记 2023.2.24 2018下半年真题

答案&#xff1a; A BIOS (BasicInputOutputSystem) (基本输入输出系统) 是一组固化到计算机内主板上一个ROM芯片上的程序&#xff0c; 它保存着计算机最重要的基本输入输出的程序&#xff0c; 开机后自检程序和系统自启动程序&#xff0c; 它可从CMOS中读写系统设置的具体信息…

SpringBoot整合(六)多数据源和 JPA、MyBatis、JdbcTemplate 的集成

在springboot项目中&#xff0c;我们可能会碰到需要多数据源的场景。例如说&#xff1a; 读写分离&#xff1a;数据库主节点压力比较大&#xff0c;需要增加从节点提供读操作&#xff0c;以减少压力。多数据源&#xff1a;一个复杂的单体项目&#xff0c;因为没有拆分成不同的…