timer库的代码示例

作者在 2014-01-30 08:24:07 发布以下内容

timer库的代码摘要如下:

class timer
{
public:
timer() { _start_time = std::clock(); }

void restart() { _start_time = std::clock(); }

double elapsed() const
{ return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }

double elapsed_min() const
{ return double(1) / double(CLOCKS_PER_SEC); }

double elapsed_max() const
{
return (double((std::numeric_limits<std::clock_t>::max)()) -
double(_start_time)) / double(CLOCKS_PER_SEC);
}
private:
std::clock_t _start_time;
};   

timer接口简单,轻巧好用,适用于大部分的程序计时任务。但使用时我们必须理解elapsed_min和elapsed_max这两个计时精度函数的含义,它们表明了timer的能力。timer不适合高精度的时间测量任务,它的精度依赖于操作系统或编译器,难以做到跨平台。timer也不适合大跨度时间段的测量。

#include <boost/timer.hpp>

#include <iostream.h>
using namespace std;
using namespace boost;
int main()
{
timer t;
cout << "max timespan: " << t.elapsed_max() / 3600 << "h" << endl;
cout << "min timespan: " << t.elapsed_min() << "s" << endl;
cout << "now time elapsed: " << t.elapsed() << "s" << endl;
return 0;
}
boost库程序设计 | 阅读 1795 次
文章评论,共0条
游客请输入验证码