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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在`chrono`中,`engine`模块提供了用于定义和模拟发动机系统的功能。该模块包含了多个类和函数,用于建模各种类型的发动机、控制发动机的输出和监测发动机性能等。 以下是一些`engine`模块中常用的类和函数: 1. `ChEngine`: 这是`chrono`中所有发动机类的基类。它提供了基本的发动机接口和功能,如设置和获取发动机参数、计算发动机输出等。你可以通过继承该基类来实现自定义的发动机模型。 2. `ChFunction_`: 这是一个函数对象类,用于定义发动机的输入函数。你可以使用不同类型的函数对象(如`ChFunction_Const`、`ChFunction_Ramp`等)来定义不同类型的输入函数,如恒定输入、斜坡输入等。 3. `ChShaft`: 这是一个表示发动机轴的类。你可以创建一个`ChShaft`对象,并将其与其他发动机组件(如曲轴、飞轮等)连接起来,以建立整个发动机系统的结构。 4. `ChShaftsEngine`: 这是一个具体的发动机模型类,继承自`ChEngine`。它实现了典型的曲轴-连杆机构和燃烧模型,用于仿真内燃机的动力学行为。你可以通过设置不同的参数(如气缸数、气缸位置、燃烧模型参数等)来创建不同类型的内燃机模型。 5. `ChShaftsMotor`: 这是另一个具体的发动机模型类,继承自`ChEngine`。它实现了一个简化的电动机模型,用于仿真电动机的动力学行为。你可以设置电动机的特性参数(如转矩-转速曲线)来模拟电动机的输出。 6. `ChShaftEngineSpeed`: 这是一个用于监测发动机转速的类。你可以创建一个`ChShaftEngineSpeed`对象,并将其与发动机轴连接,以获得发动机转速的实时信息。 除了上述类之外,`engine`模块还提供了其他一些类和函数,用于处理发动机的控制和监测,如调整发动机输入、计算发动机扭矩输出、监测发动机功率等。 需要注意的是,具体的API和使用方法可能会根据你使用的`chrono`版本和具体的代码实现有所不同。建议查阅相关文档和代码注释以获取更详细的信息。 希望这个解释能帮到你!如有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值