boost 学习笔记 2: timer

copy from:http://einverne.github.io/post/2015/12/boost-learning-note-2.html

1:timer

  头文件

  timer 位于boost命名空间,需要包含头文件 <boost/timer.hpp> 即:

#include <boost/timer.hpp>
using namespace boost;

基本方法

timer变量声明之后即开始计时工作,之后可以调用 elapsed() 方法来测量对象自创建之后所流逝的时间。成员函数 elapsed_max() 和 elapsed_min() 方法返回 timer 能够测量的时间最大值和最小值,输出单位秒。timer类的实现代码很少,可参考源码学习。

timer 实现源码

使用标准库头文件 中的 std::clock() 函数。

使用建议

  • 不适合高精度计时,精度依赖操作系统或编译器
  • 不适合大跨度时间段测量,最大跨度只有几百小时,如需要天、月甚至年做时间单位,可使用 date_time 库
 

2:progress_timer

头文件

#include <boost/progress.hpp>
using namespace boost;

基本用法

在定义 progress_timer 之后,析构对象时会自动输出流逝时间。可以使用花括号,来使 progress_timer 实现计时功能。

progress_timer t;
// do some work
cout << t.elapsed() << endl; //print out elapsed time for the task

progress_timer 精度问题,只能保留到小数点后两位,精确到百分之一秒。

3:progress_display

头文件

#include <boost/progress.hpp>
using namespace boost;

基本用法

在构造函数中传入总数,然后迭代更新进度条。

vector<string> v(100);
ofstream fs("test.txt");
progress_display pd(v.size()); // 构造函数,传入进度总数 vector<string>::iterator pos;
for (pos = v.begin(); pos != v.end(); ++pos){ // 遍历迭代,处理字符串,写文件
fs << *pos << endl;
++pd; // 更新进度
}

progress_display 问题

progress_display 向标准输出 cout 输出字符,无法将进度条和输出分离,如果循环中带有输出,则显示会很难看。

 #include <boost/timer.hpp>
#include <boost/progress.hpp>
#include <iostream>
#include <stdio.h>
using namespace boost;
using namespace std; int main()
{
timer t;
cout<<"max timespan:"<<t.elapsed_max()/<<"h"<<endl;
cout<<"min timespan:"<<t.elapsed_min()<<"s"<<endl;
cout<<"now time elapsed:"<<t.elapsed()<<"s"<<endl; //progress_timer
progress_timer t1;
cout<<t1.elapsed()<<endl; char c= getchar();
return ;
}

boost 学习笔记 2: timer的更多相关文章

  1. boost 学习笔记 0: 安装环境

    boost 学习笔记 0: 安装环境 最完整的教程 http://einverne.github.io/post/2015/12/boost-learning-note-0.html Linux 自动 ...

  2. BOOST学习笔记

    BOOST学习笔记 1 tool #pragma once #include <vector> #include "boost/noncopyable.hpp" #in ...

  3. Boost学习笔记(六) progress_display注意事项

    progress_display可以用作基本的进度显示,但它有个固有的缺陷:无法把进度显示输出与程序的输出分离. 这是因为progress_display和所有C++程序一样,都向标准输出(cout) ...

  4. Boost学习笔记(五) progress_display

    progress_display可以在控制台显示程序的执行进度,如果程序执行很耗费时间,那么它能够提供一个友好的用户界面 #include <boost\timer.hpp> #inclu ...

  5. Boost学习笔记(三) progress_timer

    progress_timer也是一个计时器,它继承自timer,会在析构时自动输出时间,省去timer手动调用elapsed()的工作,是一个用于自动计时相当方便的小工具. #include < ...

  6. Boost学习笔记(二) 时间与日期

    timer库概述 timer库包含三个组件:分别是计时器类timer.progress_timer和进度指示类progress_display timer 主要作用是计时,精确度是毫秒级.下面是一个简 ...

  7. boost学习笔记(七)---date_time库

    date_time库的日期基于格里高利历,支持从1400-01-01到9999-12-31之间的日期计算 #define BOOST_DATE_TIME_SOURCE #include <boo ...

  8. Boost学习笔记(一) 什么是Boost

    Boost库是一个功能强大.构造精巧,跨平台.开源并且完全免费的C++程序库

  9. boost 学习笔记 1: lexical_cast

    参考原著地址:http://einverne.github.io/post/2015/12/boost-learning-note-1.html 转换对象要求 lexical_cast 对转换对象有如 ...

随机推荐

  1. C# to il 9 Properties and Indexers(属性和索引器)

    A field is simply a memory location, whereas, a property is a collection of methods. Aproperty is re ...

  2. Unity3D中常用的数据结构 学习

    Array 1.数组存储在连续的内存上. 2.数组的元素都是相同类型或者类型的衍生类型 3.数组可以直接通过下标访问 ArrayList 1.不必在声明ArrayList时指定它的长度,这是由于Arr ...

  3. Could not get lock /var/lib/dpkg/lock的解决方法

    E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable) E: Unable to ...

  4. Windows git 初始设置

    主要布署在 Linux 服务器上时,将全局设置 为提交自动转为 LF,签出不转换.git config --global core.autocrlf input(无效了,按默认即可) 设置全局用户名.

  5. react-router 从 v3 版本升到 v4 版本,升级小记

    react-router v4 跟 react 一样拆成了两部分,核心的 react-router 和依运行环境而定的 react-router-dom 或 react-router-native(跟 ...

  6. 文件如何转换成pdf或html格式

    1.使用jacob插件 2.使用方法 1)于word.ppt等上传文件转换为PDF格式文件的环境搭建,步骤如下:① 首先电脑要先安装office软件(不可以是WPS软件)② 需要把jacob.dll文 ...

  7. piwik获取访客头像,自定义显示访问者头像(URL)和描述(标题和替代)

    访客头像 自定义显示访问者头像(URL)和描述(标题和替代) 链接地址:https://plugins.matomo.org/VisitorAvatar#description

  8. 如何写出健壮的Java代码

    近来在公司写代码,写出的代码发现BUG很多,为了实现一个功能,代码改了又改,影响了工单的效率,也影响个人绩效,因此从网上找了些关于写健壮代码的文章看了看,再加上自己的一些经验总结. 所谓健壮的代码是指 ...

  9. OpenEXR的读取机制

    这还是一篇学习笔记,知识重点还是领会完再敲一遍比较好. OpenEXR通过RgbaInputFile这个接口读取RGBA ONLY图像文件信息,该接口通过dataWindow()方法获取图像边界坐标信 ...

  10. 自定义自己的jQury插件

    对于一个商业插件来说,自定义插件的样式是必不可少的.我们可以通过我们自己输入不同的样式,来改变开发者的默认样式.比如说最常见的 width.height.url.color等等.要是没有这些自定义的东 ...