timer
boost中timer类,可以用来测量时间的流逝,可以提供毫秒级(依赖于操作系统,windows下为毫秒,linux下为微妙,具体可以看CLOCKS_PER_SEC宏的定义),timer的实现,依赖于标C中的clock()函数。该函数表示该程序从启动到调用该函数占用CPU的时间。
以下是boost中的源码:
#include <boost/config.hpp>
#include <ctime>
#include <boost/limits.hpp>
namespace boost
{
class timer
{
public:
timer { _start_time = std::clock();}
//重新启动定时器
void restart() { _start_time = std::clock(); }
//计算流逝的时间,已经转化为秒了,CLOCKS_PER_SEC是计时的精度
double elapsed() const
{ return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }
//输出可以计算的最大时间限度,numeric_limit<类型> 用来计算一个类型的极限,请注意调用max()和min()的写法是 (std::numeric_limits<std::clock_t>::max)(),而不是普通的std::numeric_limits<std::clock_t>max(),因为这种写法在当引入STL或者windows.h时,会编译不过。
double elapsed_max() const
{
return (double((std::numeric_limits<std::clock_t>::max)())
- double(_start_time)) / double(CLOCKS_PER_SEC);
}
double elapsed_min() const