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. python之路---11 第一类对象 函数名 闭包 迭代器

    二十九. 1.函数名的运用    ①函数名是⼀个变量, 但它是⼀个特殊的变量, 与括号配合可以执⾏函数的变量 ②函数名是一个内存地址    ③ 函数名可以赋值给其他变量         ④函数名可以当 ...

  2. java自动装箱的一个例子

    Object obj = 56; int i = (Integer)obj; 第一行等价于: Object obj = Integer.valueOf(56);      Integer.valueO ...

  3. Spring Cloud(Dalston.SR5)--Hystrix 断路器-缓存

    在 Spring Cloud 中可以使用注解的方式来支持 Hystrix 的缓存,缓存与合并请求功能需要先初始化请求上下文才能实现,因此,必须实现 javax.servlet.Filter 用于创建和 ...

  4. windows 下 Anaconda 安装 TensorFlow

    转自: https://www.cnblogs.com/nosqlcoco/p/6923861.html 什么是 Anaconda? Anaconda is the leading open data ...

  5. http修改443端口,http 强制跳转https

    修改apache http/https 端口号 1.修改http的端口 打开$HTTPD_HOME/conf/httpd.conf文件,找到Listen,后面紧跟的是端口号,默认是80,把它修改为你想 ...

  6. 《JavaScript设计模式与开发》笔记 3.call和apply

    1.改变this指向 2.Function.prototype.bind 3.借用其他对象方法 1.借用实现继承 2.实现恶心的 Array.prototype.push.call Array.pro ...

  7. 测试教程网.unittest教程.2. 基本用法

    From: http://www.testclass.net/pyunit/basic_example/ 我们通过最简单的例子来看一下unittest的基本用法,下面的代码测试了3个python字符串 ...

  8. 【mysql】之性能优化

    http://blog.csdn.net/orichisonic/article/details/48026031 https://www.cnblogs.com/zhming26/p/6322353 ...

  9. gulp学习总结

    一.gulp使用-博客推荐: http://www.sheyilin.com/2016/02/gulp_introduce/ 二.gulp的作用 gulp是一个前端构建工具,它是一个工具框架,可以通过 ...

  10. vue之v-on

    我们可以用 v-on 指令绑定一个事件监听器,通过它调用我们 Vue 实例中定义的方法: <!DOCTYPE html> <html lang="en"> ...