目录
1.timer库概述
①命名空间timer
②计时器类中用到的格式
2.类cpu_timer
①类定义
②使用例子
3.类auto_cpu_timer
①类定义
②使用例子
1.timer库概述 ^
timer是一个很小的库,提供简易的计时功能,对了解程序执行所需的时间在测试和生产环境中都很有用。
旧版本的计时器已经被弃用了,取而代之的是是更符合当前boost实践的CPU计时器cpu_timer和auto_cpu_timer。所有组件都在命名空间timer中,如下:
①命名空间timer ^
//头文件:<boost/timer/timer.hpp>
namespace boost
{
namespace timer
{
class cpu_timer;
class auto_cpu_timer;
//纳秒级别的类型
typedef boost::int_least64_t nanosecond_type;
//结构体,提供纳秒级别的经过时间,用户时间,系统时间
struct cpu_times
{
nanosecond_type wall;
nanosecond_type user;
nanosecond_type system;
void clear();
};
//默认精度为6
const int default_places = 6;
//将时间转化为指定精度,或指定精度与格式的字符串,单位为秒
std::string format(const cpu_times& times, short places, const std::string& format);
std::string format(const cpu_times& times, short places = default_places);
}
}
②计时器类中用到的格式 ^
格式字符串 | 形式 |
---|---|
%w | times.wall |
%u | times.user |
%s | times.system |
%t | times.user + times.system |
%p | times.wall,times |