boost.timer:一个优秀的计时类库

来源:互联网 发布:统计学excel数据分析 编辑:程序博客网 时间:2024/06/05 18:27
在编写程序的时候很多种情况下我们都需要计时,比如我们在测试软件的性能时,或者一个和时间有关的软件时boost.timer就特别有用,boost.timer提供两个类cpu_timerauto_cpu_timer,它们都包含在boost::timer命名空间内。

auto_cpu_timer

废话不多说先看一个例子:

// if on win32 this make the program auto link to dynme library#define BOOST_ALL_DYN_LINK #include <boost/timer/timer.hpp>void func(some arguments){    ...}int main(void){    // a boost.timer class auto_cpu_time    boost::timer::auto_cpu_time timer(“%w clock time,%t totle program time(%p%)”);for (int i = 0;i < 10000;i++){        ...        func(some arguments);        ...}
}

程序运行结果

0.318385 clock time,0.312002 totle program time(98.0%)

我们看到代码中仅仅声明了一个boost::timer::auto_cpu_timer类,for循环的运行时间(包括clock time即世界时间,和cpu time即程序在cpu中运行的时间)就被计算出来了,究其原因,我们大概可以猜想到auto_cpu_timer在构造阶段调用了计时相关的函数,在析构阶段结束计时,并以一定的格式输出结果,这其中有一个细节就是:定义在区块中的自动变量(auto value)都会在离开区块是时动析构,这里的析构对于内建的类型如int char等就是释放栈空间,对于用户定义类型就是调用析构函数后释放空间。上述过程是C++语言规定的,它由C++编译器强制保证。

正如auto_cpu_timer的名字一样,我们只需要简单的定义一个auto_cpu_timer变量便可方便的计时了,auto_cpu_timer有六个构造函数:

explicit auto_cpu_timer(short places = default_places);     // #1auto_cpu_timer(short places, const std::string& format);    // #2explicit auto_cpu_timer(const std::string& format);         // #3auto_cpu_timer(std::ostream& os, short places,       const std::string& format)                   // #4explicit auto_cpu_timer(std::ostream&os,                short places = default_places);     // #5auto_cpu_timer(std::ostream& os, const std::string& format) // #6

os

其中的std::ostream指向一个输出流这就意味着可以将输出结果定位到任意输出流了如标准输出,日志文件等等

format

其中的std::string 是一个格式化字符串就如printf函数中的格式化字符串一样不过具体的格式如下:

%w

wall clock time指的是世界时间比如钟表的时间

%u

user time指的是用户代码执行时间 即程序在用户态时间

%s

system time指的是系统代码执行时间即程序在内核中的时间

%t

user time + system time

%p

he peacetion of the totle time in wall time程序总时间在世界时间中的百分比(由于cpu的线程切换,totle time总是不大于wall time

places

places指的是保留的秒数小数点的位数,默认的是8位,超过8为的会被忽略并置为8位小于0的也会被忽略,并会被置为0

cup_timer

事实上auto_cpu_timer继承于cpu_time,但是我们在简单的情况下都是运用auto_cpu_timer的自动机制的,如果情况复杂就应该用cup_time

cup_time有三个动作:starvoid)、stopvoid)和resumevoid)分别负责从当前时间点启动计时、停止计时,和从上一个计时启动点重新开始计时。

有三个状态函数is_stopedvoid)、elapsedvoid)和format..., is_stop指示计时是否已经停止,elapsed返回当前计时以用时间但是不停止计时,format返回当前计时以用时间的字符串形式,不停止计时。

cup_timer的声明代码大致如下:

class BOOST_TIMER_DECL cpu_timer{  public:    //  constructor    cpu_timer() { start(); }    //  observers    bool        is_stopped() const  { return m_is_stopped; }    cpu_times  elapsed() const;  // does not stop()    std::string   format(short places, const std::string& format) const;    std::string   format(short places = default_places) const    //  actions    void          start();    void          stop();    void          resume();   private:    cpu_times     m_times;    bool          m_is_stopped;};

其中保存时间的结构体cpu_times定义如下:

typedef boost::int_least64_t nanosecond_type;struct cpu_times{    nanosecond_type wall;    nanosecond_type user;    nanosecond_type system;    void clear() { wall = user = system = 0LL; }};

其中的boost::int_least64_t 事实上是__int64类型的

下面代码展示了cup_time的基本用法:

#define BOOST_ALL_DYN_LINK#include <boost/timer/timer.hpp>void fun(void){    for (int i = 0;i < 1000;i++);}int main(void){    boost::timer::cpu_timer timer;    for (int i = 0;i < 100000;i++)        fun();    std::cout << timer.format(5,"%ws wall time,%ts totle time\n");        boost::timer::cpu_times time = timer.elapsed();    std::cout << "wall time is :" << time.wall << "ns("               << time.wall/1000000000.0L << "s)" << std::endl;    return 0;}

boost.timer事实上实现起来并不困难,只是由于跨平台,需要调用不同的系统API来得到精密的时间而已,运用boost.timer我们可以在大多数的Linux/Unix系统和win32系统上运行。

原创粉丝点击