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;
}


你可能感兴趣的:(timer,OS,Class)