boost::timer库

转自:boost::中文翻译文档 地址:http://code.google.com/p/boost-doc-zh/downloads/list

只为了记录自己学习过程

timer库作用:度量时间,进度显示功能

timer库包含三个组件:1.计时器类(timer、progress_timer)、2进度指示类(progress_display)

一、timer:

  Timer类用于测量流逝的时间。对于那些小型的计时任务,它通常都很方便。相比较依赖C标准库的clock()函数那未知的准确度和精密度, timer提供的实现具有适度的可移植性。在timer本身的实现里面,最大可测量的时间长度大概只有596.5小时(甚至更短)。由于这些限制,不能指望这个timer类非常的健壮(robust),并且也不应该使用在有这方面考虑的任务上。

  类摘要:

#include <boost/timer.hpp>
namespace boost {
class timer {
public:
timer(); // 执行之后: elapsed()==0
// compiler generated copy constructor, copy assignment, and dtor apply编译器生成的拷贝构造函数、拷贝赋值函数、和析构函数
void restart(); // 执行之后: elapsed()==0
double elapsed() const; // 以秒为单位,返回流逝的时间

double elapsed_max() const; // 返回 elapsed() 的最大估计值
// 关于可移植性的警告:在某些系统上面,std::clock_t类型溢出或者重置,这样elapsed_max()可能会返回一个非常大的值。

double elapsed_min() const; // 返回 elapsed() 的最小值
}; // timer
} // namespace boost

EG:

timer t0; 

cout << "timer::elapsed_min() reports " << t0.elapsed_min() << " seconds\n";
cout << "timer::elapsed_max() reports " << t0.elapsed_max()
<< " seconds, which is " << t0.elapsed_max()/3600.0 << " hours\n";

二、progress_timer

  progress_timer类自动地测量流逝的时间,并且析构的时候在一个适当的地方以一种适当的形式,显示一个流逝了多少时间的消息。它所提供的实现默认是以字符的显示方式输出到 std::cout。

  类摘要:

#include <boost/progress.hpp>
namespace boost {
class progress_timer : public timer, noncopyable {
public:
progress_timer();
progress_timer( std::ostream& os ); // os只是一个提示;具体的实现可能会忽略掉
~progress_timer();
}; // progress_display
} // namespace boost

EG:

progress_timer类通常用于对程序的执行计时。你可以像这样简单的使用它:

#include <boost/progress.hpp>
int main()
{
progress_timer t; // 开始计时
// 做一些别的事情 ...
return 0;
}

这段代码就会产生类似下面这样的输出:

1.23 s
三、progress_display

  progress_display类在适当的地方以适当的形式显示出相对于预设目标的当前进度。这可以让用户知道一个程序是否正在进展之中。
类摘要:
#include <boost/progress.hpp>
namespace boost {
class progress_display : noncopyable {
public:
progress_display( unsigned long expected_count );
// 效果: restart(expected_count)

progress_display( unsigned long expected_count,
std::ostream& os, // os只是一个提示;具体的实现可能会忽略掉
const std::string & s1 = "\n", // 行首字符串(leading strings)
const std::string & s2 = "",
const std::string & s3 = "" )
// 效果: 保存行首字符串(leading strings)的一份拷贝,然后restart(expected_count)

void restart( unsigned long expected_count );
// 效果: 用三行显示合适的比例,并且每行分别以从构造函数传入的本地拷贝的s1, s2, s3的开头。
// 执行之后: count()==0, expected_count()==expected_count

unsigned long operator+=( unsigned long increment )
// 效果: Display appropriate progress tic if needed.
// 执行之后: count()== original count() + increment
// 返回值: count().

unsigned long operator++()
// 返回值: operator+=( 1 ).

unsigned long count() const
// 返回值: The internal count.

unsigned long expected_count() const
// 返回值: The expected_count from the constructor.

}; // progress_display
} // namespace boost

EG:

比如,如果要在叫一个做big_map的std::map<>容器上进行一个很耗时的计算,下面的代码就可以显示出当前的进度:

  progress_display show_progress( big_map.size() );
for ( big_map_t::iterator itr = big_map:begin();
itr != big_map.end(); ++itr )
{
// 进行计算
...
++show_progress;
}

在70%的元素被处理完之后,显示出来的效果如下所示:

0%   10   20   30   40   50   60   70   80   90   100%
|----|----|----|----|----|----|----|----|----|----|
************************************

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2012-06-09 09:24  ocean0606  阅读(180)  评论(0)    收藏  举报