Boost库学习------progress_timer
2013-09-13 15:17
417 查看
1.progress_timer继承自timer,实现了timer的所有功能,但是比timer更加简单,不用显示调用elapsed()函数来显示时间间隔,它的析构函数自动调用了elapsed(),也就是说当它创建后到析构这段时间会自动显示。
int main(){
progress_timer pt;
..........
}
当pt的作用于结束时,自动输出创建到销毁的时间。
还可以记录多个作用于的时间,例如:
int main(){
{
progress_timer pt1;
}
...........
{
progress_timer pt2;
}
}
progress_timer源码十分简洁:
可以看到,progress_timer用一个特定的流进行初始化,默认为标准输出流。当对象被析构时会把时间间隔自动输出到指定的流中。
从代码中可以看到,输出只精确到了小数点后2位,如果我们需要更精确的输出,根据开放封闭原则,可以继承progress_timer,从而改变输出精度。
int main(){
progress_timer pt;
..........
}
当pt的作用于结束时,自动输出创建到销毁的时间。
还可以记录多个作用于的时间,例如:
int main(){
{
progress_timer pt1;
}
...........
{
progress_timer pt2;
}
}
progress_timer源码十分简洁:
class progress_timer : public timer, private noncopyable { public: explicit progress_timer( std::ostream & os = std::cout ) : m_os(os) {} ~progress_timer() { // A) Throwing an exception from a destructor is a Bad Thing. // B) The progress_timer destructor does output which may throw. // C) A progress_timer is usually not critical to the application. // Therefore, wrap the I/O in a try block, catch and ignore all exceptions. try { // use istream instead of ios_base to workaround GNU problem (Greg Chicares) std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed, std::istream::floatfield ); std::streamsize old_prec = m_os.precision( 2 ); m_os << elapsed() << " s\n" // "s" is System International d'Unites std << std::endl; m_os.flags( old_flags ); m_os.precision( old_prec ); } catch (...) {} // eat any exceptions } // ~progress_timer private: std::ostream & m_os; };
可以看到,progress_timer用一个特定的流进行初始化,默认为标准输出流。当对象被析构时会把时间间隔自动输出到指定的流中。
从代码中可以看到,输出只精确到了小数点后2位,如果我们需要更精确的输出,根据开放封闭原则,可以继承progress_timer,从而改变输出精度。
相关文章推荐
- 小羊驼和你一起学习cocos2d-x之三(进度条、CCProgressTimer)
- cocos2d-x学习(5)-------CCProgressTimer(进度条)
- boost库之时间处理(timer,progress_timer, progress_display)
- Boost库的progress_timer和progress_display讲解
- Boost库学习随记二 date_time、time_duration、date_facet、time_facet、timer库示例等:
- Boost库学习随记一 timer库示例:
- CCProgressTimer学习
- coco2dx 学习笔记 -- 进度动画 ccProgressTimer ccProgressTo ccProgressFromTo
- 【08】 Boost库学习笔记之定时器(Timer)
- Boost库学习----timer
- boost学习2.3:progress_timer类(继承自timer)
- Activiti boundrytimer 和listen结合使用学习
- IOS学习笔记29—提示框第三方库之MBProgressHUD
- Java学习之任务调度--Timer
- iOS学习——利用Timer更新通话时间与播放器进度条
- Cocos2d-x CCProgressTimer
- boost库学习笔记
- 1- 开始学习 context-M3 LM3S1138 上传timer 的学习资料
- Boost库的timer类讲解
- TimerTask代码示例[学习]