chrono 模块学习

三大类

顶级clock: system_clock日期时间; steady_clock启动时间, 硬件震动clock;

  • 内置四大类型: duration使用精度类型(min,sec,ms,ns等 加 数量); rep记录对应精度数量的类型(默认longlong, 也可以自定义其他类型); period: 精度, 即当前精度等于1s*ratio, (ms = 1 / 1000s); time_point<system_clock>: 用system_clock作为表达方式的时刻;
  • 一个成员变量: is_steady是否高精度;
  • 若干生成器; now, to_time_t, from_time_t, 即如何用系统的时间机制生成STL对象;

duration: 一段时间, 适用于日期计算;

即精度 + 精度数量; ms * 10001000ms;

time_point: 时刻点 = clock + duration, 适用于高精度程序执行耗时计算;

即起始时刻可以是1970.1.1为起始用于日期计算; 也可以是电脑开机时间为起始用于时间计算;

总结

  • 时间起点: epoch
  • duration: ratio * count, 转换规则count * ratio_new / ratio_old;
  • timepoint: epoch + duration
  • clock: 从系统获取对应类型值, 用于生成匹配类型;

clock

system_clock: 可变化计算量

定义了四种类型; 一个静态成员, 三个对象生成器;

class _LIBCPP_TYPE_VIS system_clock
{
public:typedef microseconds                     duration;typedef duration::rep                    rep;typedef duration::period                 period;typedef chrono::time_point<system_clock> time_point;static constexpr const bool is_steady = false;static time_point now() _NOEXCEPT;static time_t     to_time_t  (const time_point& __t) _NOEXCEPT;static time_point from_time_t(time_t __t) _NOEXCEPT;
};

steady_clock: 启动时间, 常量;

class _LIBCPP_TYPE_VIS steady_clock
{
public:typedef nanoseconds                                   duration;typedef duration::rep                                 rep;typedef duration::period                              period;typedef chrono::time_point<steady_clock, duration>    time_point;static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;static time_point now() _NOEXCEPT;
};

high_resolution_clock: 即二选一, 优先steady_clock;

#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
class _LIBCPP_TYPE_VIS steady_clock
{
public:typedef nanoseconds                                   duration;typedef duration::rep                                 rep;typedef duration::period                              period;typedef chrono::time_point<steady_clock, duration>    time_point;static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;static time_point now() _NOEXCEPT;
};typedef steady_clock high_resolution_clock;
#else
typedef system_clock high_resolution_clock;
#endif

总结: 支持单位不同换算

  • 两种clock定义了四种成员类型;
  • system_clock, steady_clock主要差异在使用精度(microseconds, nanoseconds)不同;
  • 类型分别是: 时长类型, 数学计数类型, 当前使用精度; 时间点;
  • duration = rep + period, time_point = clock + duration, time_point可以是时间日期, 也可以是启动时间点;

获取数据

获取对应精度的数量

duration::count()

不同精度的转换

template <class _ToDuration, class _Rep, class _Period>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
typename enable_if
<__is_duration<_ToDuration>::value,_ToDuration
>::type
duration_cast(const duration<_Rep, _Period>& __fd)
{return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
}template <class _ToDuration, class _Clock, class _Duration>
inline constexpr time_point<_Clock, _ToDuration>
time_point_cast(const time_point<_Clock, _Duration>& __t)
{return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
}

类型转换原理

  • 存储数量的算数类型转换;
  • 存储精度的转换;

精度转换并四舍五入: c++17


#if _LIBCPP_STD_VER > 14
template <class _ToDuration, class _Clock, class _Duration>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
typename enable_if
<__is_duration<_ToDuration>::value,time_point<_Clock, _ToDuration>
>::type
floor(const time_point<_Clock, _Duration>& __t)
{return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
}template <class _ToDuration, class _Clock, class _Duration>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
typename enable_if
<__is_duration<_ToDuration>::value,time_point<_Clock, _ToDuration>
>::type
ceil(const time_point<_Clock, _Duration>& __t)
{return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
}template <class _ToDuration, class _Clock, class _Duration>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
typename enable_if
<__is_duration<_ToDuration>::value,time_point<_Clock, _ToDuration>
>::type
round(const time_point<_Clock, _Duration>& __t)
{return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
}template <class _Rep, class _Period>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
typename enable_if
<numeric_limits<_Rep>::is_signed,duration<_Rep, _Period>
>::type
abs(duration<_Rep, _Period> __d)
{return __d >= __d.zero() ? +__d : -__d;
}
#endif

总结

日期计算: system_clock; 耗时计算: steady_clock

duration: 支持各种精度记录时间;

// 算数类型 + second 的 n 倍精度;
typedef duration<long long,         nano> nanoseconds;
typedef duration<long long,        micro> microseconds;
typedef duration<long long,        milli> milliseconds;
typedef duration<long long              > seconds;
typedef duration<     long, ratio<  60> > minutes;
typedef duration<     long, ratio<3600> > hours;template <intmax_t _Num, intmax_t _Den = 1>
class ratio;// 分数精度, 常用单位s
typedef ratio<1LL, 1000000000000000000LL> atto;
typedef ratio<1LL,    1000000000000000LL> femto;
typedef ratio<1LL,       1000000000000LL> pico;
typedef ratio<1LL,          1000000000LL> nano;
typedef ratio<1LL,             1000000LL> micro;
typedef ratio<1LL,                1000LL> milli;
typedef ratio<1LL,                 100LL> centi;
typedef ratio<1LL,                  10LL> deci;
typedef ratio<                 10LL, 1LL> deca;
typedef ratio<                100LL, 1LL> hecto;
typedef ratio<               1000LL, 1LL> kilo;
typedef ratio<            1000000LL, 1LL> mega;
typedef ratio<         1000000000LL, 1LL> giga;
typedef ratio<      1000000000000LL, 1LL> tera;
typedef ratio<   1000000000000000LL, 1LL> peta;
typedef ratio<1000000000000000000LL, 1LL> exa;

count()获取对应单位的时刻

不同精度转换;clock之间转换:, duration之间转换:duration_cast; time_point之间转换:time_point_cast;

参考链接

https://en.cppreference.com/w/cpp/chrono

案例

时间: 系统时间转换, clock之间计算返回结果duration, 之间类型转换并获取结果;

#include<chrono>
#include <iostream>
#include<cstdlib>
#include<unistd.h>
int main() {//std::cout << std::chrono::system_clock::now().time_since_epoch().count() << std::endl;auto start = std::chrono::system_clock::now();sleep(1);auto end = std::chrono::system_clock::now();auto dif = end - start;std::cout << std::chrono::round<std::chrono::seconds>(dif).count() << std::endl;
}

建议用high_resolution_clock计算两个点程序执行时间差duration;

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

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

相关文章

【数据结构】 二叉树面试题讲解->贰

文章目录 &#x1f30f;引言&#x1f384;[二叉树遍历](https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef?tpId60&&tqId29483&rp1&ru/activity/oj&qru/ta/tsing-kaoyan/question-ranking)&#x1f431;‍&#x1f464;题目描述&#…

Java 创建一个线程的方式

Java中创建线程主要有三种方式&#xff0c;分别为继承Thread类、实现Runnable接口、实现Callable接口。 继承Thread类&#xff0c;重写run()方法&#xff0c;调用start()方法启动线程 public class ThreadTest {/*** 继承Thread类*/public static class MyThread extends Thr…

1.(python数模)单函数读取常用文件

Python单函数读取常用文件 代码如下&#xff1a; import pandas as pd# 读取数据文件 def readDataFile(readPath): # readPath: 数据文件的地址和文件名try:if (readPath[-4:] ".csv"):dfFile pd.read_csv(readPath, header0, sep",") # 间隔符为逗…

音频——I2S TDM 模式(六)

I2S 基本概念飞利浦(I2S)标准模式左(MSB)对齐标准模式右(LSB)对齐标准模式DSP 模式TDM 模式 文章目录 TDM formatTDM format ATDM format BTDM format C总结 TDM format TDM 分为两种常用操作模式&#xff1a;TDM A mode 和 TDM B mode, 统称为TDM mode 基于 TDM mode&#x…

leetcode做题笔记114. 二叉树展开为链表

给你二叉树的根结点 root &#xff0c;请你将它展开为一个单链表&#xff1a; 展开后的单链表应该同样使用 TreeNode &#xff0c;其中 right 子指针指向链表中下一个结点&#xff0c;而左子指针始终为 null 。展开后的单链表应该与二叉树 先序遍历 顺序相同。 思路一&#x…

buuctf [CISCN 2019 初赛]Love Math

这题主要利用了php的一些特性 变量函数数字转字符串 源码 <?php error_reporting(0); //听说你很喜欢数学&#xff0c;不知道你是否爱它胜过爱flag if(!isset($_GET[c])){show_source(__FILE__); }else{//例子 c20-1$content $_GET[c];if (strlen($content) > 80) {…

【App端】uni-app使用百度地图api和echarts省市地图下钻

目录 前言方案一&#xff1a;echarts百度地图获取百度地图AK安装echarts和引入百度地图api完整使用代码 方案二&#xff1a;echarts地图和柱状图变形动画实现思路完整使用代码 方案三&#xff1a;中国地图和各省市地图下钻实现思路完整使用代码 前言 近期的app项目中想加一个功…

Uniapp笔记(四)uniapp语法3

一、商品详情 1、从商品列表页跳转到商品详情页 在商品列表的项中绑定单击事件&#xff0c;并传递商品id值 <view class"goods-item" v-for"(item,index) in goodsList" :key"index" click"goGoodsDetail(item.goods_id)"> &…

新版Mongodb(6.0以上)找不到mongo.exe

安装目录下/bin目录中&#xff0c;没有mongo.exe文件&#xff0c;只有mongod和mongos&#xff0c;以及一个powershell命令脚本。 原因在于&#xff0c;mongodb6.0以后做出了重大改变&#xff0c;mongodb已经不再默认为你安装shell工具&#xff0c;因此需要安装一个额外的shell…

FFmpeg5.0源码阅读——FFmpeg大体框架(以GIF转码为示例)

摘要&#xff1a;前一段时间熟悉了下FFmpeg主流程源码实现&#xff0c;对FFmpeg的整体框架有了个大概的认识&#xff0c;因此在此做一个笔记&#xff0c;希望以比较容易理解的文字描述FFmpeg本身的结构&#xff0c;加深对FFmpeg的框架进行梳理加深理解&#xff0c;如果文章中有…

基于负载均衡的在线OJ实战项目

前言&#xff1a; 该篇讲述了实现基于负载均衡式的在线oj&#xff0c;即类似在线编程做题网站一样&#xff0c;文章尽可能详细讲述细节即实现&#xff0c;便于大家了解学习。 文章将采用单篇不分段形式&#xff08;ps&#xff1a;切着麻烦&#xff09;&#xff0c;附图文&#…

javacv 基础04-读取mp4,avi等视频文件并截图保存图片到本地

javacv 读取mp4,avi等视频文件并截图保存图片到本地 代码如下&#xff1a; package com.example.javacvstudy;import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.Frame; import org.bytedeco.javacv.Java2DFrameConverter;import javax.imageio.Im…

Macos 10.13.2安装eclipse

eclipse for php 安装2021-12最后版本4.22 2021-12 R | Eclipse Packages jdk17 x64 dmg安装包,要安装jdk这个才能运行 Java Downloads | Oracle

Prompt2Model: Generating Deployable Models from Natural Language Instructions

本文是LLM系列文章&#xff0c;针对《 Prompt2Model: Generating Deployable Models from Natural Language Instructions》的翻译。 Prompt2Model&#xff1a;从自然语言指令生成可部署模型 摘要1 引言2 Prompt2Model框架3 参考实现4 实验设置5 实验结果6 讨论与结论不足道德…

wangluobiancheng

UDP send: receive: TCP

函数式编程(四)Stream流使用

一、概述 在使用stream之前&#xff0c;先理解Optional 。 Optional是Java 8引入的一个容器类&#xff0c;用于处理可能为空的值。它提供了一种优雅的方式来处理可能存在或不存在的值&#xff0c;避免了空指针异常。 Optional的主要特点如下&#xff1a; 可能为空&#xff…

朝夕光年游戏自动化测试实践

朝夕光年是面向全球用户与开发者的游戏研发与发行业务品牌&#xff0c;致力于服务全球玩家&#xff0c;帮助玩家在令人惊叹的虚拟世界中一起玩耍与创造。 在游戏的研发过程中&#xff0c;游戏自动化一直是开展难度较大的工程&#xff0c;具体包括机房机架、设备调度、软件框架、…

IntelliJ IDEA 简介

IntelliJ IDEA 简介 IntelliJ IDEA&#xff08;简称 IDEA&#xff09;是一款由 JetBrains 公司开发的强大且广受欢迎的集成开发环境&#xff08;IDE&#xff09;&#xff0c;主要用于Java开发&#xff0c;但也支持其他编程语言和技术栈的开发。作为一款功能丰富、高效的IDE&am…

理解底层— —Golang的log库,二开实现自定义Logger

理解底层— —Golang的log库&#xff0c;实现自定义Logger 1 分析实现思路 基于golang中自带的log库实现&#xff1a;对日志实现设置日志级别&#xff0c;每天生成一个文件&#xff0c;同时添加上前缀以及展示文件名等 日志级别&#xff0c;通过添加prefix&#xff1a;[INFO]、…

学生信息管理系统MIS(前端)

改造HTML文件 <!DOCTYPE html> <html><head><meta charset"utf-8"><title>学生信息管理系统MIS</title><!-- link在HTML文件中,引入外部的css文件 rel的值是固定写法,stylesheet样式表href用来指定样式表的位置--><lin…