C++11 chrono duration_cast ratio 

概览

c++新标准提供了新的线程库,最近在写测试代码的时候需要让当前线程休眠,之前直接调用windows提供的Sleep()就好了,新标准中可以使用std::this_thread::sleep_for()或者std::this_thread::sleep_until()
来实现休眠。其中涉及到了std::chrono::durationstd::chrono::time_point。本篇只总结std::chrono::durationstd::chrono::time_point会再写一篇总结。

std::chrono::duration

描述

std::chrono::duration定义在文件中,用来表示一个时间段。

cppreference上的原话如下:

Class template std::chrono::duration represents a time interval.
It consists of a count of ticks of type Rep and a tick period, where the tick period is a compile-time rational constant representing the number of seconds from one tick to the next.
The only data stored in a duration is a tick count of type Rep. If Rep is floating point, then the duration can represent fractions of ticks. Period is included as part of the duration's type, and is only used when converting between different durations.

Rep参数代表了可以传入的时间单位的类型,可以为float, int, int64等等,如果为float表示可以传入时间单位的一部分,比如传入1.2表示1.2倍个时间单位
Period参数代表了时间单位,可以为微秒,毫秒,秒,分钟,小时等(或者其它自定义的单位,类型为std::ratio)。

注:

  1. 上文中的tick可以理解为周期,或时间单位。
  2. the number of seconds 表示是周期值基于秒来计算的。

duration_cast()分析

函数duration_cast()提供了在不同的时间单位之间进行转换的功能。

duration_cast()主要分为两部分:

  • 通过ratio_divide定义了从一个ratio转换到另外一个ratio的转换比例。
    比如1/102/5的转换比例是1/4 ((1/10/(2/5)) = 1/4),也就是说一个1/10相当于1/42/5
    对应到代码里就是_CF::num = 1, _CF::den = 4.

  • 根据转换比例把n个单位的原数据转换到目标数据(return语句)
    return语句写的这么复杂是为了效率,避免不必要的乘除法,当分子是1的时候没必要乘,当分母是1的时候没必要除。
    简化一下(去掉了强制类型转换)就是:
    return _Dur.count() * (_CF::num / _CF::den);

通俗点讲:如果AB的转换比例是num/den,那么1A可以转换为num/denBnA可以转换为 n * (num/den)B

注:vs自带的源码真心不易读,推荐参考boost源码。

预定义的duration

vs为了写代码方便,预定义了几个常用的时间单位,摘录如下:

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<int, ratio<60> > minutes; // 分钟 
typedef duration<int, ratio<3600> > hours; // 小时

根据以上定义我们可以发现std::chrono::microseconds定义中的Rep的类型是long longPeriod类型是milli

注:因为std::chrono::microseconds定义中的Rep的类型是long long, 我们不能通过如下方法来休眠100.5毫秒std::this_thread::sleep_for(std::chrono::microseconds(100.5));,类型不匹配,会报编译错误。如果想休眠100.5毫秒,我们可以这么写:
std::this_thread::sleep_for(std::chrono::duration<float, std::milli>(100.5f));

示例代码

例1:分钟转换为毫秒

#include <iostream> 
#include <chrono> 
int main() 
{ 
    std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::minutes(3)); 
    std::cout << "3 minutes equals to " << ms.count() << " milliseconds\n"; 
    std::cin.get(); 
} 

例2. 自定义单位转换

#include <iostream> 
#include <chrono> 
 
typedef std::chrono::duration<float, std::ratio<3, 1> > three_seconds; 
typedef std::chrono::duration<float, std::ratio<1, 10> > one_tenth_seconds; 
 
int main() 
{ 
    three_seconds s = std::chrono::duration_cast<three_seconds>(one_tenth_seconds(3)); 
    std::cout << "3 [1/10 seconds] equal to " << s.count() << " [3 seconds]\n"; 
    std::cin.get(); 
} 

例3. 休眠100毫秒

#include <thread> 
#include <chrono> 
int main() 
{ 
    std::this_thread::sleep_for(std::chrono::milliseconds(100)); 
    // or 
    std::this_thread::sleep_for(std::chrono::duration<long long, std::milli>(100)); 
    // or   
    // typedef ratio<1, 1000> milli; 
    std::this_thread::sleep_for(std::chrono::duration<long long, std::ratio<1, 1000> >(100)); 
} 

 

std::chrono 3种时钟用法

std::chrono是C++11引入的日期时间处理库,其中包含3种时钟:system_clock,steady_clock,high_resolution_clock。近来需要使用高精度时间,很自然想到使用high_resolution_clock,然而使用后发现并非预期的得到自1970/1/1零点之后的计数,而是一个小得多的数字。那么这三种时钟有什么区别,用在什么情况下,我们来一探究竟。

问题

auto tp = std::chrono::high_resolution_clock::now();
std::cout << tp.time_since_epoch().count() << std::endl;

上述代码输出一个比较小的数字,high_resolution_clock的精度是纳秒,不可能是这么小的数字。

三种时钟的区别

所谓时钟,是指从一个时点开始,按照某个刻度的一个计数。如下代码摘自VC2017。

  • system_clock
struct system_clock
{	// wraps GetSystemTimePreciseAsFileTime/GetSystemTimeAsFileTime
typedef long long rep;
typedef ratio_multiply<ratio<_XTIME_NSECS_PER_TICK, 1>, nano> period;
typedef chrono::duration<rep, period> duration;
typedef chrono::time_point<system_clock> time_point;
static constexpr bool is_steady = false;

对于system_clock,其起点是epoch,即1970-01-01 00:00:00 UTC,其刻度是1个tick,也就是_XTIME_NSECS_PER_TICK纳秒。

  • high_resolution_clock
typedef steady_clock high_resolution_clock;

high_resolution_clock实际上和steady_clock一样。

  • steady_clock
struct steady_clock
{	// wraps QueryPerformanceCounter
typedef long long rep;
typedef nano period;
typedef nanoseconds duration;
typedef chrono::time_point<steady_clock> time_point;
static constexpr bool is_steady = true;

steady_clock的刻度是1纳秒,起点并非1970-01-01 00:00:00 UTC,一般是系统启动时间,这就是问题的关键。steady_clock的作用是为了得到不随系统时间修改而变化的时间间隔,所以凡是想得到绝对时点的用法都是错误的。steady_clock是没有to_time_t()的实现的,而system_clock是有的。

三种时钟用在什么时候

  • system_clock:用在需要得到绝对时点的场景
auto tp = std::chrono::system_clock::now();
std::time_t tt = std::chrono::system_clock::to_time_t(tp);
std::cout << tt << "seconds from 1970-01-01 00:00:00 UTC" << std::endl;
  • steady_clock:用在需要得到时间间隔,并且这个时间间隔不会因为修改系统时间而受影响的场景
auto tp1 = std::chrono::steady_clock::now();
//do something
auto tp2 = std::chrono::steady_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(tp2 - tp1).count() << "microseconds" << std::endl;
  • high_resolution_clock:high_resolution_clock是system_clock或steady_clock之一,根据情况使用

常见的错误用法

  • std::this_thread::sleep_until传入的是steady_clock::time_point
  • 根据steady_clock::time_point得到time_t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值