boost库之时间处理(timer,progress_timer, progress_display)

1.timer

#include "stdafx.h"
#include <iostream>

#include "boost/timer.hpp"
//using namespace boost;

int _tmain(int argc, _TCHAR* argv[])
{
	boost::timer t;

	std::cout << t.elapsed_max() << std::endl; //可度量的最大时间
	std::cout << t.elapsed_min() << std::endl; //可度量的最小时间,以秒为单位
	std::cout << t.elapsed() << std::endl;	   //输出流逝的时间
	return 0;
}


2.progress_timer

#include "stdafx.h"

#include "boost/progress.hpp"
#include "boost/static_assert.hpp"

//精度可控制
template<int N = 2>
class new_progress_timer:public boost::timer
{
public:
	new_progress_timer(std::ostream& os = std::cout)
		: m_os(os)
	{
		BOOST_STATIC_ASSERT(N >= 0 && N <= 10);
	}

	~new_progress_timer()
	{
		try
		{
			std::istream::fmtflags old_flags = m_os.setf(std::istream::fixed, std::istream::floatfield);
			std::streamsize old_prec = m_os.precision(N);

			m_os << elapsed() << " s\n"
				<< std::endl;

			m_os.flags(old_flags);
			m_os.precision(old_prec);
		}
		catch (...)
		{
		}
	}
private:
	std::ostream& m_os;
};

int _tmain(int argc, _TCHAR* argv[])
{
	new_progress_timer<10> t;
	::Sleep(1010);
	return 0;
}

3.progress_display

#include "stdafx.h"

#include "boost/progress.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
	//在控制台上显示程序执行进度
	boost::progress_display t(1000);
	for (int i = 0; i < 1000; i++)
	{
		::Sleep(10);
		++t;
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值