[原]C++新标准之std::chrono::duration
概览
c++新标准提供了新的线程库,最近在写测试代码的时候需要让当前线程休眠,之前直接调用windows提供的Sleep()就好了,新标准中可以使用std::this_thread::sleep_for()或者std::this_thread::sleep_until()
来实现休眠。其中涉及到了std::chrono::duration和std::chrono::time_point。本篇只总结std::chrono::duration,std::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)。
注:
- 上文中的tick可以理解为周期,或时间单位。
- the number of seconds 表示是周期值基于秒来计算的。
类定义
std::chrono::duration是一个模板类,关键代码摘录如下(格式有调整):
- template<class _Rep, class _Period>
- class duration {
- public:
- typedef duration<_Rep, _Period> _Myt;
- typedef _Rep rep;
- typedef _Period period;
- // constructor, save param to _MyRep, used by count() member function.
- template<class _Rep2,
- class = typename enable_if<is_convertible<_Rep2, _Rep>::value
- && (treat_as_floating_point<_Rep>::value || !treat_as_floating_point<_Rep2>::value),
- void>::type>
- constexpr explicit duration(const _Rep2& _Val)
- : _MyRep(static_cast<_Rep>(_Val))
- {
- }
- constexpr _Rep count() const { return (_MyRep); }
- };
- // convert duration from one unit to another.
- template<class _To, class _Rep, class _Period> inline
- constexpr typename enable_if<_Is_duration<_To>::value, _To>::type
- duration_cast(const duration<_Rep, _Period>& _Dur)
- {
- typedef ratio_divide<_Period, typename _To::period> _CF;
- typedef typename _To::rep _ToRep;
- typedef typename common_type<_ToRep, _Rep, intmax_t>::type _CR;
- #pragma warning(push)
- #pragma warning(disable: 6326) // Potential comparison of a constant with another constant.
- return (_CF::num == 1 && _CF::den == 1
- ? static_cast<_To>(static_cast<_ToRep>(_Dur.count()))
- : _CF::num != 1 && _CF::den == 1
- ? static_cast<_To>(static_cast<_ToRep>(
- static_cast<_CR>(
- _Dur.count()) * static_cast<_CR>(_CF::num)))
- : _CF::num == 1 && _CF::den != 1
- ? static_cast<_To>(static_cast<_ToRep>(
- static_cast<_CR>(_Dur.count())
- / static_cast<_CR>(_CF::den)))
- : static_cast<_To>(static_cast<_ToRep>(
- static_cast<_CR>(_Dur.count()) * static_cast<_CR>(_CF::num)
- / static_cast<_CR>(_CF::den))));
- #pragma warning(pop)
- }
duration_cast()分析
函数duration_cast()
提供了在不同的时间单位之间进行转换的功能。
duration_cast()
主要分为两部分:
通过
ratio_divide
定义了从一个ratio转换到另外一个ratio的转换比例。
比如1/10
到2/5
的转换比例是1/4
((1/10/(2/5)) = 1/4),也就是说一个1/10
相当于1/4
个2/5
。
对应到代码里就是_CF::num = 1, _CF::den = 4
.根据转换比例把n个单位的原数据转换到目标数据(return语句)
return
语句写的这么复杂是为了效率,避免不必要的乘除法,当分子是1的时候没必要乘,当分母是1的时候没必要除。
简化一下(去掉了强制类型转换)就是:
return _Dur.count() * (_CF::num / _CF::den);
通俗点讲:如果A
到B
的转换比例是num/den
,那么1
个A
可以转换为num/den
个B
, n
个A
可以转换为 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 long
,Period
类型是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));
- }
参考资料
- VS源码
- cppreference
[原]C++新标准之std::chrono::duration的更多相关文章
- [原]C++新标准之std::chrono::time_point
原 总结 STL 标准库 chrono time_point ratio 概览 类定义 总结 思考 拓展 system_clock steady_clock high_resolution_cloc ...
- [原]C++新标准之std::ratio
原 总结 ratio 概览 类定义 预定义ratio 应用 示例代码 参考资料 概览 std::ratio定义在<ratio>文件中,提供了编译期的比例计算功能.为std::chrono ...
- [原]C++新标准之std::thread
原 总结 C++11 thread 概览 std::thread 类定义 各个成员函数的简单介绍 例子 更多 参考资料 概览 从C++11开始提供了线程的支持,终于可以方便的编写跨平台的线程代码了. ...
- C++11 std::chrono库详解
所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解. chrono是一个time library, 源于boost,现在已经是C++标准.话说今年似乎又 ...
- c++11 时间类 std::chrono
概念: chrono库:主要包含了三种类型:时间间隔Duration.时钟Clocks和时间点Time point. Duration:表示一段时间间隔,用来记录时间长度,可以表示几秒钟.几分钟或者几 ...
- std::chrono计算程序运行时间
void CalRunTime() { auto t1=std::chrono::steady_clock::now(); //run code auto t2=std::chrono::steady ...
- C++11新特性,利用std::chrono精简传统获取系统时间的方法
一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...
- C++ 11新特性:std::future & std::shared_future) (转载)
上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...
- c++11 标准库函数 std::move 和 完美转发 std::forward
c++11 标准库函数 std::move 和 完美转发 std::forward #define _CRT_SECURE_NO_WARNINGS #include <iostream> ...
随机推荐
- bne 1b 汇编含义
汇编指令中 bne label 这条指令有以下两种特别的写法:bne 1b, bne 1f. bne 1b 指的是 backward,倒退寻找标号为 1 的地方并跳转. 同样也有 bne 1f,值得是 ...
- BUUCTF-WEB-easy_tornado
知识点: Python Web 框架:Tornado python中的一个渲染函数:render: 渲染变量到模板中,即可以通过传递不同的参数形成不同的页面. 1. render方法的实质就是生成te ...
- soupui--替换整个case的url
添加新的URL 随便进入一个case的[REST]step,添加新的url 更换URL 添加完之后双击想要更换url的case,在弹出的窗口中点击URL按钮 在弹出的set endpoint窗口中选择 ...
- 获取ExtJS中开启Form.fileUpload时,返回的信息
当Form,开启fileUpload后,不能按默认的方式得到action.result,开启fileUpload与否,返回的action.result的内容是不一样的 未开启fileUpload时,返 ...
- vscode-wechat 小程序开发提示工具 vscode 安装
vscode 安装 vscode-wechat vscode-wechat 小程序开发提示工具 ---- 有了小程序开发提示,开发很方便 https://segmentfault.com/a/1190 ...
- Delphi生成即调用带窗体的Dll
library frmDll; { Important note about DLL memory management: ShareMem must be the first unit in you ...
- 视频课程 | Kubernetes的兴起
视频课程 | Kubernetes的兴起 原创: 京小云 京东云开发者社区 4月3日 京东云开发者社区在3月底于北京举行了以"Cloud Native时代的应用之路与开源创新"为 ...
- cmd 删除整个目录
rmdir 删除整个目录好比说我要删除 222 这个目录下的所有目录和档案,这语法就是: rmdir /s/q 222 其中: /s 是代表删除所有子目录跟其中的档案. /q 是不要它在删除档案或目录 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: this 关键字
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Json返回结果为null属性不显示解决
import java.io.IOException; import org.springframework.boot.autoconfigure.condition.ConditionalOnMis ...