现在的位置: 首页 > 综合 > 正文

boost timer类介绍收藏

2013年07月01日 ⁄ 综合 ⁄ 共 1390字 ⁄ 字号 评论关闭
  boost timer类介绍收藏


新一篇: boost lambda简介 | 旧一篇: boost库xml序列化


1, 简介

  很多时候我们写程序都要计算一下时间. 其实就是在一个操作两端记录一下时
  间, 然后减一下. 如果我们懒到这个动作都想省掉, 那就用boost::timer吧.

2, 使用

  有三个可以用的类

类progress_timer在离开定义自己的scope之后就会自动销毁, 并且显示经过的时间.

#include <boost/progress.hpp>
#include <boost/timer.hpp>
#include <windows.h>
#include <iostream>

int main()
{
        {
                boost::progress_timer t;  // start timing
                boost::timer tx;
                Sleep(1000);
                std::cout << tx.elapsed() << std::endl;
        }
        std::cout << "Next round" << std::endl;
        {
                boost::progress_timer j;  // start timing
                Sleep(1000);
        }
    return 0;
}

类progress_display显示一个进度条, 好好玩哦.

#include <boost/progress.hpp>
#include <windows.h>
#include <iostream>

int main()
{
        boost::progress_display pd(13);
        for (int i = 0; i< 13; i++) {
                Sleep(100);
                ++pd;
        }
    return 0;
}

类timer, 显得比以上两个稍微正式一点. 例子就嵌在上面的程序里面了.
当然你可以在需要的时候用timer.reset重置计时器.

 

发表于 @ 2006年09月23日 02:23:00|评论(0)|编辑


新一篇: boost lambda简介 | 旧一篇: boost库xml序列化

抱歉!评论已关闭.