首页 > 编程语言 >【C++11】chrono库

【C++11】chrono库

时间:2023-01-12 12:00:10浏览次数:39  
标签:11 ratio chrono C++ 时间段 value using 模板 精度

chrono是c++11中的时间库,提供计时、时钟等功能
学习chrono,关键是理解里面 精度时间段时间点的概念

1. 精度

时钟节拍(时间精度),后面的时间段和时间点都是基于精度的计算
模板定义

template <intmax_t _Nx, intmax_t _Dx = 1>
struct ratio { // holds the ratio of _Nx to _Dx
    static_assert(_Dx != 0, "zero denominator");
    static_assert(-INTMAX_MAX <= _Nx, "numerator too negative");
    static_assert(-INTMAX_MAX <= _Dx, "denominator too negative");

    static constexpr intmax_t num =
        _Sign_of<_Nx>::value * _Sign_of<_Dx>::value * _Abs<_Nx>::value / _Gcd<_Nx, _Dx>::value;

    static constexpr intmax_t den = _Abs<_Dx>::value / _Gcd<_Nx, _Dx>::value;

    using type = ratio<num, den>;
};

_Nx表示分子,_Nx表示分母,单位默认是秒,用来表示精度。比如ratio<1, 1000>表示的精度是1/1000秒,即1毫秒
_Gcd<_Nx, _Dx>::value表示_Nx和_Dx的最大公约数
ratio<_Nx,_Dx>::num表示分子除以最大公约数的商
ratio<_Nx,_Dx>::den表示分母除以最大公约数的商
ratio<_Nx,_Dx>::type表示该类型的定义,之所以与最大公约数进行相除取商,然后重新使用type指向ratio<num,den>是因为,两者是等价的都表示的是同一精度,num/den=(num*_Gcd<_Nx,_Dx>::value)/(den*_Gcd<_Nx,_Dx>::value)=_Nx/_Dx

比如下面测试用例

// using milli = ratio<1, 1000>;        // 系统提供的模板特化类,精度是毫秒
using my_milli = ratio<10, 10000>;      // 自己实现的模板特化类,精度也是毫秒
std::cout << std::milli::num << "/" << std::milli::den << std::endl;    // 打印:1/1000
std::cout << my_milli::num << "/" << my_milli::den << std::endl;        // 打印:1/1000        
std::cout << typeid(std::milli::type).name() << std::endl;              // 打印:struct std::ratio<1,1000>
std::cout << typeid(my_milli).name() << std::endl;                      // 打印:struct std::ratio<10,10000>   
std::cout << typeid(my_milli::type).name() << std::endl;                // 打印:struct std::ratio<1,1000>

可以看出,上面milli和my_milli,虽然模板特化类不一样,但是实际提供的精度是一致的,都是1/1000
补充上系统常见定义的一些精度,注意,没有专门定义精度为秒的模板特化

using micro = ratio<1, 1000000>;
using milli = ratio<1, 1000>;
using centi = ratio<1, 100>;
using deci  = ratio<1, 10>;

2. 时间段

构造时间段的模板定义如下

 template <class _Rep, class _Period>
    class duration { // represents a time duration
    public:
        using rep    = _Rep;
        using period = typename _Period::type;
        // xxxx
    };

这里_Rep表示一种数值类型,比如int、float、double等,用来存储_Period的数量
_Period就是我们上面提到的ratio<_Nx,_Dx>模板特化类型,一般默认是使用std::ratio<1>表示精度是秒。
综上,时间段就是某个精度的连续个数,比如数量是100个,精度是秒,则表示时间段是100秒。数量是1.2,精度是秒,则表示时间段是1.2秒。

可以通过下面的代码来观察


3. 时间点

标签:11,ratio,chrono,C++,时间段,value,using,模板,精度
From: https://www.cnblogs.com/ganshang/p/17013912.html

相关文章

  • sc stream-rabbit 简化版20230112
     二、消费者【2058】    1、pom.xml        <dependency>            <groupId>org.springframework.cloud</groupId>    ......
  • 全志T113芯片蓝牙音乐播放失败如何解决?
    1.主题T113-S3蓝牙音乐播放失败问题2.问题背景硬件:T113+XR829软件:Linux(非Tina)3.问题描述3.1复现步骤#驱动路径要根据固件实际路径insmod/lib/modules/5.4.61±a......
  • 5月TIOBE编程语言:Python持续第一,C++将冲击前三
    新的TIOBE5月编程语言榜单出炉了,让我们一起看一下这次有哪些新看点:Python稳居第一,C++或将冲至Top3本次的榜单和4月相比没有明显变化,Top5依然是Python、C、Java、C++......
  • 2023-01-11 多头萌发使用中出现的问题
    首先看一下大级别,也就是你做的级别5分钟的大2个级别,也就是4h,4h是铁丝网形态。注意这种形态出现的时候,最好的交易是不交易。   30分钟是一个上升线段。   最......
  • 算法--2023.1.11
    1.力扣146--LRU缓存classLRUCache{classNode{publicintkey;publicintvalue;publicNodepre,next;publicNode(){}......
  • C++ STL的简单应用(vector容器专题)
    #include<iostream>#include<string>#include<stdlib.h>#include<vector>//#include<algorithm>usingnamespacestd;//vector容器的简单应用voiddemo1(){......
  • c++ unit test via gtest
    //model/book.h#pragmaonce#include<iostream>usingnamespacestd;classbook{public:book()=default;~book()=default;book(constbook&......
  • C++ 使用 new 创建二维数组
    1.直接创建C++使用new创建二维数组最直接的方法就是newT[M][N]。返回的指针类型是T(*)[N],它是指向数组的指针,可以直接使用数组下标形式访问元素。释放内存直接使......
  • 1.11刷题记录
    1.Yesecnodrumsticks1根据题目提示得知本题是lsb隐写打开附件得到一张图片用stegsolve打开flag{Yesec_1s_lsb}2.qsdz'sgirlfriend1根据题目提示得知:flag......
  • 力扣11. 盛最多水的容器(贪心)
    给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i,0) 和 (i,height[i]) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可......