C++ 计时器:chrono库介绍


C++11有了chrono库,可以在不同系统中很容易的实现定时功能。

要使用chrono库,需要#include,其所有实现均在std::chrono namespace下。注意标准库里面的每个命名空间代表了一个独立的概念。

chrono是一个模版库,使用简单,功能强大,只需要理解三个概念:duration、time_point、clock

一 、时钟CLOCK
chrono库定义了三种不同的时钟:

1 std::chrono::system_clock: 依据系统的当前时间 (不稳定) 2 std::chrono::steady_clock: 以统一的速率运行(不能被调整) 3 std::chrono::high_resolution_clock: 提供最高精度的计时周期(可能是steady_clock或者system_clock的typedef)

二、这三个时钟有什么区别呢?
system_clock就类似Windows系统右下角那个时钟,是系统时间。明显那个时钟是可以乱设置的。明明是早上10点,却可以设置成下午3点。

steady_clock则针对system_clock可以随意设置这个缺陷而提出来的,他表示时钟是不能设置的。

high_resolution_clock则是一个高分辨率时钟。

这三个时钟类都提供了一个静态成员函数now()用于获取当前时间,该函数的返回值是一个time_point类型,

system_clock除了now()函数外,还提供了to_time_t()静态成员函数。用于将系统时间转换成熟悉的std::time_t类型,得到了time_t类型的值,在使用ctime()函数将时间转换成字符串格式,就可以很方便地打印当前时间了。

1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<ctime>//将时间格式的数据转换成字符串 5 #include<chrono> 6 using namespace std::chrono; 7 using namespace std; 8 int main() 9

三、持续的时间 duration
td::chrono::duration<int,ratio<60,1>> ,表示持续的一段时间,这段时间的单位是由ratio<60,1>决定的,int表示这段时间的值的类型,函数返回的类型还是一个时间段duration

std::chrono::duration<double,ratio<60,1>>

由于各种时间段(duration)表示不同,chrono库提供了duration_cast类型转换函数。

duration_cast用于将duration进行转换成另一个类型的duration。

duration还有一个成员函数count(),用来表示这一段时间的长度

1 #include<iostream> 2 #include<string.h> 3 #include<chrono> 4 using namespace std::chrono; 5 using namespace std; 6 int main() 7

四、时间点 time_point
std::chrono::time_point 表示一个具体时间,如上个世纪80年代、你的生日、今天下午、火车出发时间等,只要它能用计算机时钟表示。鉴于我们使用时间的情景不同,这个time point具体到什么程度,由选用的单位决定。一个time point必须有一个clock计时

设置一个时间点:

std::time_point<clock类型> 时间点名字

1 //设置一个高精度时间点 2 std::time_point<high_resolution_clock> high_resolution_clock::now();
1 //设置系统时钟 2 std::chrono::time_point<std::chrono::system_clock> now=std::chrono::system_clock::now();

另一个实例:

1 #define _CRT_SECURE_NO_WARNINGS //localtime()需要这个宏 2 #include<iostream> 3 #include<chrono> 4 #include<vector> 5 #include<string> 6 #include<algorithm> 7 //#include<stdio.h> 8 9 #include<ianip> //put_time需要的头文件 10 11 #include<sstream> 12 13 // template <class _Rep> 14 // struct treat_as_floating_point : is_floating_point<_Rep> ; // tests for floatingpoint type 15 16 // template <class _Rep> 17 // _INLINE_VAR constexpr bool treat_as_floating_point_v = treat_as_floating_point<_Rep>::value; 18 19 // // STRUCT TEMPLATE duration_values 20 // template <class _Rep> 21 // struct duration_values 26 27 // _NODISCARD static constexpr _Rep(min)() noexcept 31 32 // _NODISCARD static constexpr _Rep(max)() noexcept 36 // }; 37 38 //时间长度 39 void Func1() 51 if(c1==c3) 54 if(c2==c3) 57 58 //获取时钟周期的值,返回的是int整数 59 std::cout<<"c1= "<<c1ount()<<std::endl; 60 std::cout<<"c2= "<<c2ount()<<std::endl; 61 std::cout<<"c3= "<<c3ount()<<std::endl; 62 std::cout<<"c4= "<<c4ount()<<std::endl; 64 std::chrono::seconds c7(1); //1秒 65 std::chrono::milliseconds c8(1*1000); //1000毫秒;1s 66 std::chrono::microseconds c9(1*1000*1000); //1000*1000微秒 1s 67 std::chrono::nanoseconds c10(1*1000*1000*1000);//1000*1000*1000纳秒 1s; 68 69 std::cout<<c7ount()<<std::endl; 70 std::cout<<c8ount()<<std::endl; 71 std::cout<<c9ount()<<std::endl; 72 std::cout<<c10ount()<<std::endl; 73 } 74 75 //系统时间 76 /** 77 * @brief 78 * 79 * system_clock 类支持了对系统时钟的访问, 提供了三个静态成员函数: 80 返回当前时间的时间点。 81 static std::chrono::time_point<std::chrono::system_clock> now() noexcept; 82 将时间点 time_point 类型转换为 std::time_t 类型。 83 static std::time_t to_time_t( const time_point& t ) noexcept; 84 将 std::time_t 类型转换为时间点 time_point 类型。 85 static std::chrono::system_clock::time_point fr_time_t( std::time_t t ) noexcept; 86 */ 87 88 void Func2() 114 115 // struct steady_clock 135 // }; 136 137 // using high_resolution_clock = steady_clock; 138 // } // namespace chrono 139 140 //计时器 steady_clock 类相当于秒表,操作系统只要启动就会进行时间的累加,常用于耗时的统计(精确到纳秒) 。 141 void Func3(); 147 std::for_each(vec1.begin(),vec1.end(),[&vec1](std::string str)); 150 std::cout<<std::endl; 151 //静态成员函数std::chrono::steady_clock::now()获取时间的结束点 152 auto end=std::chrono::steady_clock::now(); 153 154 //计算消耗的时间,单位是纳秒 155 auto dt=endstart; 156 std::cout<<"耗时: "<<dtount()<<"纳秒 ("<<(double)dtount()/(1000*1000*1000)<<"秒) "<<std::endl; 157 158 } 159 160 int main(int argc,char* argv[])

输出结果:

1 PS D:\时间操作 chrono 库\bin\Debug> .\main.exe 2 c1==c2 3 c1==c3 4 c2==c3 5 c1= 1 6 c2= 60 7 c3= 3600 8 c4= 3600000 9 1 10 1000 11 1000000 12 1000000000 13 20230104 22:32:43 14 20230104 15 22:32:43 16 20230104 22:32:43 17 banana apple pear 18 耗时: 733纳秒 (0.0007334秒) 19 PS D:\时间操作 chrono 库\bin\Debug>



上一篇:Expat XML解析库

下一篇:蔡司激光共聚焦荧光显微镜 (五)


C/C&#x2B;&#x2B;
Copyright © 2002-2019 k262电脑网 www.k262.cn 皖ICP备2020016292号
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!QQ:251442993 热门搜索 网站地图