boost库学习——第二节 timer简单好用计时器

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值