Boost库-Chrono

会有一股神秘感。 提交于 2020-02-26 22:20:53
//不编译使用system库
#define BOOST_ERROR_CODE_HEADER_ONLY
//不编译使用chrono库
#define BOOST_CHRONO_HEADER_ONLY
//使用chrono库的扩展项
#define BOOST_CHRONO_EXTRNSIONS

/*
chrono中定义了时间长度表达duration
duration的类摘要如下:
//默认模板参数单位是秒
template<class Rep,class Period = rario<1>>
class duration
{
public:
//内部类型定义
    typedef Rep rep;
	typedef Period period
//成员
private:
//时间单位计数
    rep rep_;
//
public:
//构造函数
    constexpr duration( );
	constexpr explicit duration(const Rep& r);
	constexpr duration(const duration &d);
    constexpr rep count( ) const;
	duration& opreator++( );
	duration& opreator+=(const duration& d);
	duration& opreator*=(const rep& rhs)
	……
	static constexpr duration zero( );//零值
	static constexpr duration min( );//最小值
	static constexpr duration max( );

}

时间参照:
//纳秒
typedef duration<boost::int_least64_t,nano> ns;
//微秒
typedef duration<boost::int_least64_t,micro> ms;
//秒
typedef duration<boost::int_least64_t> s;
//分
typedef duration<boost::int_least32_t,ratio<60>> m;
//时
typedef duration<boost::int_least32_t,ratio<3600>> h;
//
typedef duration<boost::int_least32_t,ratio<3600*24>> d;

时间转换:
chrono库中提供了一个模板函数 duration_cast( )来转换不同的时间单位
它执行简单的舍入取整

时钟:
system_clock : 如实反应计算因为系统实际时间的时钟
steady_clock:稳定的时钟,不会因系统时间调整而变化
high_resolution_clock:高分辨率的时钟,但通常是前两种的typedef

chrono库还定义四个度量程序运行时间时钟:
process_real_cpu_clock
process_user_cpu_clock
process_system_cpu_clock
thread_clock

system_clock类摘要
class system_clock
{
public:
//时间单位
    typedef some_define duration;
//时间计数
	typedef duration::rep rep;
//
    typedef duration::peroid peroid;
//时间点
	typedef chrono::time_point time_point;
//是否是稳定
    static constexpr bool is_steady;
//获取当前时间
    static  time_point now( );
//转化为c里的time_t结构
    static std::time_t to_time_t(const time_point& t);
	static time_point from_time_t(const std::time_t& t);

}

时间点
时间点time_point与时钟紧密相连,由一个时钟产生,标记自时钟起点以来的
所经过的时间time_point的类摘要如下:

template<class Clock,class Duration = typename colock::duration>
class time_point
{
public:
    typedef Clock clock;
	typedef typename duration::rep rep;
	typedef typename duration::period period;

private:
//时间长度
    duration d_;
//
public:
    constexpr time_point min( );
	 constexpr time_point max( );
#ifdef BOOST_CHRONO_EXTENSIONS
    constexpr time_point  operator+( );
	……
	……
    time_point& opreator -= (const rep& d);
#endif
}

*/

#include<boost\chrono.hpp>
#include<iostream>

//纳秒
typedef boost::chrono::duration<boost::int_least64_t, boost::nano> c_ns;
//微秒
typedef boost::chrono::duration<boost::int_least64_t, boost::micro> c_ms;
//秒
typedef boost::chrono::duration<boost::int_least64_t> c_s;
//分
typedef boost::chrono::duration<boost::int_least32_t, boost::ratio<60>> c_m;
//时
typedef boost::chrono::duration<boost::int_least32_t, boost::ratio<3600>> c_h;
//天
typedef boost::chrono::duration<boost::int_least32_t, boost::ratio<3600 * 24>> c_d;

//高精度计时器
class steady_timer final
{
private:
	typedef boost::chrono::steady_clock clock_type;
	typedef clock_type::time_point time_point_type;
	//使用秒精度
	typedef boost::chrono::microseconds duration_type;

	time_point_type m_start = clock_type::now();

public:
	steady_timer() = default;
	~steady_timer() = default;

public:
	void ReStart()
	{
		m_start = clock_type::now();
	}

	duration_type Elapsed() const
	{
		return  boost::chrono::round<c_ms>(clock_type::now() - m_start);
	}

};



//6自定义时间面值
c_h operator"" _h(unsigned long long n)
{
	return c_h(n);
}

c_m operator"" _m(unsigned long long n)
{
	return c_m(n);
}

c_s operator"" _s(unsigned long long n)
{
	return c_s(n);
}


int main(int argc, char** argv)
{

	//1 测试时间单位
	c_s s1(5);

	assert(s1.count() == 5);

	std::cout << s1 << std::endl;


	//2 类型转换
	c_s s2(30);

	auto m2 = boost::chrono::duration_cast<c_m>(s2);

	std::cout << m2 << std::endl;

	//3 标准的扩展函数
	c_s s3(3600 + 50);

	std::cout << boost::chrono::floor<c_m>(s3) << std::endl;
	std::cout << boost::chrono::ceil<c_m>(s3) << std::endl;
	std::cout << boost::chrono::round<c_m>(s3) << std::endl;
	std::cout << boost::chrono::round<c_h>(s3) << std::endl;

	//4 时钟信息输出
	std::cout<<boost::chrono::clock_string<boost::chrono::system_clock,char>::name() << std::endl;
	std::cout << boost::chrono::clock_string<boost::chrono::system_clock, char>::since( )<< std::endl;

	std::cout << boost::chrono::clock_string<boost::chrono::steady_clock, char>::name() << std::endl;
	std::cout << boost::chrono::clock_string<boost::chrono::steady_clock, char>::since() << std::endl;

	std::cout << boost::chrono::clock_string<boost::chrono::process_real_cpu_clock, char>::name() << std::endl;
	std::cout << boost::chrono::clock_string<boost::chrono::process_real_cpu_clock, char>::since() << std::endl;

	//5 时间点
	auto tp1 = boost::chrono::system_clock::now();
	std::cout << "当前时间:" <<tp1<< std::endl;

	auto d = tp1.time_since_epoch();
	std::cout << "自起点到当前时间长度:" << d << std::endl;

	//转化
	std::cout << boost::chrono::duration_cast<c_d>(d) << std::endl;

	//6 自定义时间面值
	auto h = 5_h;
	std::cout << h << std::endl;

	//7 高精度计时器
	steady_timer stimer;


	std::cout<<stimer.Elapsed()<<std::endl;

	

	return 0;
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!