通过progress_timer组件扩展计时精度
来源:互联网 发布:2017中日贸易数据 编辑:程序博客网 时间:2024/04/29 18:13
//通过progress_timer扩展计时精度//原则上程序库的代码不能被用户修改,不过我们可以通过模板技术仿造progress_timer编写一个新类//new_progress_timer以实现任意精度的输出#include <iostream>#include <boost/progress.hpp>#include <boost/static_assert.hpp> //静态断言,控制精度范围在0~10以内using namespace boost;using namespace std;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()//析构函数是核心功能,保存IO流状态,然后设定输出精度,完成输出后恢复IO流的状态{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;};//使用模板特例化,精度为2template<>class new_progress_timer<2>:public boost::progress_timer{};int main(){new_progress_timer<10> t;//精度为10unsigned int i;int sum = 1;for(i = 0; i < 1000000000; ++i)//10亿条指令sum %= 2;}
运行结果:
2.6560000000 s
- 通过progress_timer组件扩展计时精度
- boost 计时器精度扩展 progress_timer 计时器扩展 类模版
- progress_timer
- 纳秒级的计时精度
- 纳秒级的计时精度
- VBA 通过timer计时
- C/C++计时方法及精度
- C#实现精确计时,精度1ms
- 扩展精度算法
- 组件的设计时属性
- DELPHI高精度计时方法,取毫秒级时间精度
- 一种提高51单片机计时精度的算法
- 关于1ms精度的计时延时器的基本测试
- BCB设计时组件编译错误
- 通过DTE获取设计时开发环境
- progress_timer和progress_display
- Boost库学习------progress_timer
- boost ---- boost::progress_timer
- 主流数据库字段类型总结
- C#输出SQL 2005表中所有记录!...
- 西游记,做人的道理
- hdu2001
- hdu2002
- 通过progress_timer组件扩展计时精度
- mysql 的max_connections和max_user_connections 的区别
- hdu2003
- C++使用template时报“有多个重载函数 与参数列表匹配”错误
- Fedora安装Nidia显卡驱动
- STEINER POINT
- Fedora 16安装MPlayer 播放器
- 二进制“<<”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)
- java list