C++11时间操作chrono库

C++11 提供了 计时码表 模版库,实现了一系列时间相关的操作(时间长度、系统时间和计时器)。
头文件:#include < chrono>
命名空间:std::chrono

一、时间长度

duration 模板类用于表示一段时间(时间长度、时钟周期),如:1 小时、8 分钟、5 秒。
duration 的定义如下:
template<class Rep, class Period = std::ratio<1, 1>>
class duration
{
......
};

为了方便使用,定义了一些常用的时间长度,比如:时、分、秒、毫秒、微秒、纳秒,它们都位于
std::chrono 命名空间下,定义如下:

using hours = duration<Rep, std::ratio<3600>> // 小时
using minutes = duration<Rep, std::ratio<60>> // 分钟
using seconds = duration<Rep> // 秒
using milliseconds = duration<Rep, std::milli> // 毫秒
using microseconds = duration<Rep, std::micro> // 微秒
using nanoseconds = duration<Rep, std::nano> // 纳秒

注意:
 duration 模板类重载了各种算术运算符,用于操作 duration 对象。
 duration 模板类提供了 count()方法,获取 duration 对象的值。

#include <iostream>
#include < 计时码表> 计时码表 库的头文件。
using namespace std;
int main()
{
计时时:小时 t1(1); // 1 小时
计时时程::分钟 t2(60); // 60 分钟
计时时码表::秒 t3(60 * 60); // 60*60 秒
计时时程::毫秒 t4(60 * 60 * 1000); // 60*60*1000 毫秒
计时::微秒 T5(60 * 60 * 1000 * 1000); // 警告:整数溢出。
计时::纳秒 T6(60 * 60 * 1000 * 1000*1000); // 警告:整数溢出。
if (t1 == t2) cout << "t1==t2\n";
if (t1 == t3) cout << "t1==t3\n";
if (t1 == t4) cout << "t1==t4\n";
// 获取时钟周期的值,返回的是 int 整数。
cout << "t1=" << t1.count() << endl;
cout << "t2=" << t2.count() << endl;
cout << "t3=" << t3.count() << endl;
cout << "t4=" << t4.count() << endl;
计时::秒 T7(1); // 1 秒
计时时程::毫秒 t8(1000); // 1000 毫秒
计时::微秒 T9(1000 * 1000); // 1000*1000 微秒
计时::纳秒 T10(1000 * 1000 * 1000); // 1000*1000*1000 纳秒
if (t7 == t8) cout << "t7==t8\n";
if (t7 == t9) cout << "t7==t9\n";
if (t7 == t10) cout << "t7==t10\n";
// 获取时钟周期的值。
cout << "t7=" << t7.count() << endl;
cout << "t8=" << t8.count() << endl;
cout << "t9=" << t9.count() << endl;
cout << "t10=" << t10.count() << endl;
}

二、系统时间

system_clock 类支持了对系统时钟的访问,提供了三个静态成员函数:
// 返回当前时间的时间点。

system_clock 类支持了对系统时钟的访问,提供了三个静态成员函数:
// 返回当前时间的时间点。

static std::chrono::time_point<std::chrono::system_clock> now() noexcept;
// 将时间点 time_point 类型转换为 std::time_t 类型。
static std::time_t to_time_t( const time_point& t ) noexcept;
// 将 std::time_t 类型转换为时间点 time_point 类型。
static std::chrono::system_clock::time_point from_time_t(std::time_t t )
#define _CRT_SECURE_NO_WARNINGS // localtime()需要这个宏。
#include <iostream>
#include < 计时码表>
#include <iomanip> // put_time()函数需要包含的头文件。
#include <sstream>
using namespace std;
int main()
{
// 1)静态成员函数 chrono::system_clock::now()用于获取系统时间。(C++时间)
auto now = chrono::system_clock::now();
// 2)静态成员函数 计时时::system_clock::to_time_t()把系统时间转换为 time_t。(UTC 时间)
自动t_now = chrono::system_clock::to_time_t(现在);
// t_now = t_now + 24*60*60; // 把当前时间加 1 天。
// t_now = t_now + -1*60*60; // 把当前时间减 1 小时。
// t_now = t_now + 120; // 把当前时间加 120 秒。
// 3)std::localtime()函数把 time_t 转换成本地时间。(北京时)
// localtime()不是线程安全的,VS 用 localtime_s()代替,Linux 用 localtime_r()代替。
auto tm_now = std::localtime(&t_now);
// 4)格式化输出 tm 结构体中的成员。
std::cout << std::put_time(tm_now, "%Y-%m-%d %H:%M:%S") << std::endl;
std::cout << std::put_time(tm_now, "%Y-%m-%d") << std::endl;
std::cout << std::put_time(tm_now, "%H:%M:%S") << std::endl;
std::cout << std::put_time(tm_now, "%Y%m%d%H%M%S") << std::endl;
stringstream ss; // 创建 stringstream 对象 ss,需要包含<sstream>头文件。
ss << std::put_time(tm_now, "%Y-%m-%d %H:%M:%S"); // 把时间输出到对象 ss 中。
string timestr = ss.str(); // 把 ss 转换成 string 的对象。
cout << timestr << endl;
}

三、计时器

steady_clock 类相当于秒表,操作系统只要启动就会进行时间的累加,常用于耗时的统计(精确到
纳秒)。

#include <iostream>
#include < 计时码表>
using namespace std;
int main()
{
// 静态成员函数 计时时::steady_clock::现在()获取开始的时间点。
自动启动 = chrono::steady_clock::now();
// 执行一些代码,让它消耗一些时间。
cout << "计时开始 ...... \n";
for (int ii = 0; ii < 1000000; ii++) {
// cout << "test...\n";
}
cout << "计时完成 ...... \n";
// 静态成员函数 计时时::steady_clock::现在()获取结束的时间点。
自动结束 = chrono::steady_clock::now();
// 计算消耗的时间,单位是纳秒。
auto dt = end - start;
cout << "耗时: " << dt.count() << "纳秒("<<(double)dt.count()/(1000*1000*1000)<<"
秒)";
}

  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值