Boost库简单运用——时间与日期的处理(二)

在上文中,我们了解了一种对时间进行处理的boost类,今天我们就来学习另一种时间处理的类——progress_timer,它继承于timer类,拥有timer类的功能,又在其易用性上更进一步,接下来我们以代码对该类进行说明:

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

int main()
{
    boost::progress_timer pt1;
    std::cout << pt1.elapsed_max() << std::endl;
    std::cout << pt1.elapsed_min() << std::endl;
    std::cout << pt1.elapsed() << std::endl;

    {
        boost::progress_timer pt2;
    }

    return 0;
}

既然继承于timer,那timer的各种公有方法当然也是可以使用的。在阅读代码时,各位可能会有疑问,在代码末尾定义了一个实例pt2,却不对它进行任何操作,这有什么实际用途么?答案是:有,因为progress_timer在构造时自动开始计时,析构时将结果输出到流中,由输出流对外输出。所以它比timer更加方便,我们无需执行任何操作,一切由其自身执行。代码中这种块作用域的声明方式对于同一份代码中测试多种算法也是比较有用的。

最后,我们需要注意的一点是,progress_timer精度直到小数点后两位,处理普通时间问题应该比较轻松。若需要更多精度,则需要自己进一步定制了。

时间: 2024-07-30 08:47:48

Boost库简单运用——时间与日期的处理(二)的相关文章

Boost库简单运用——时间与日期的处理(一)

对于时间与日期的处理一直都是一个比较复杂的问题,而C++中对于时间与日期的处理也是比较简陋的,并不像Java.C#之流提供了非常方便易用的类.但随着Boost的推出,对于时间的处理也变得越来越简单方便了,今天我们就来学习一些较为方便的处理方式: 首先,我们先来了解Boost中对时间处理的一个比较基础的类,也是后续几个类的基类--timer. timer类可以测量时间的流逝,依据平台的不同,提供了毫秒甚至微秒级别的时间控制,我们也可以用它来做一个很简单的计时器,下面,我们通过代码来解释timer的

Boost库简单运用——时间与日期的处理(三)

今天我们就开始boost的日期处理部分,日期有太多需要细细考虑的地方,遇到日期处理问题时,自己写一个处理类,那显然是极其耗时的.学习了今天的内容之后,相信我们在日期处理时将更加得心应手.今天我们学习的是boost日期处理的核心类--date.同样,下面我们从代码入手进行学习: #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main() { boost::gregorian::da

(一)boost库之日期、时间

(一)boost库之日期.时间 一.计时器 计时器,通常在一个项目中统计一个函数的执行时间是非常实用的. #include <boost/timer.hpp> void PrintUserTime() { boost::timer t; //定义一个计时类,开始计时 std::cout << "可度量的最大时间:" << t.elapsed_max()/3600 << "h" << std::endl; s

Boost库 对时间和日期的处理 date_timer库

/*Boost 对时间和日期的处理 提供了timer和data_time 库*/ //有关timer库提供了简易的度量时间和进度显示的功能可以用于性能测试等需要计时的任务 /* timer 的三个组件  计时器类timer  progress_timer和进度指示类progress_display timer 可测量时间的流逝,提供毫秒级的计时精确度 #include<boost\timer.hpp> #include<iostream> using namespace boost

c++ boost库学习一:时间和日期

timer类 #include <boost\timer.hpp> #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { boost::timer t; cout<<"max time span: "<<t.elapsed_max()/3600<<"h"<<endl; //596.5

简单编写makefile文件,实现GCC4.9编译项目,加入boost库测试等等。。

一.需要用到的hw.cpp hw.h funtest.cpp funtest.h makefile 几个测试文件 1.hw.cpp代码如下: #include "hw.h" #include "funtest.h" using namespace std; using namespace boost; int main() { timer t; { int i=1; } auto i="abc"; cout<<i<<end

[Boost]boost的时间和日期处理-(1)日期的操作

<开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1400年以后的任何Gregorian日历日期.如果你需要计算再早的日期,则需要寻求其他库来支持. 日期和时间是编程过程中常用的操作.在C标准库中,<time.h>提供了time_t类型.和tm结构类型的时间日期相关函数.Windows API也提供了FILETIME类型的相关函数.由于这里是介绍b

【ThinkingInC++】48、用标准C库中的时间函数生成简单的Time类

Cpptime.h /** * 书本:[ThinkingInC++] * 功能:用标准C库中的时间函数生成简单的Time类 * 时间:2014年9月11日07:53:56 * 作者:cutter_point */ #ifndef CPPTIME_H_INCLUDED #define CPPTIME_H_INCLUDED #include<ctime> #include<cstring> //这里ctime和cstring是不包含using namespace std;的而后面加.h

【c++】简单模拟实现boost库下的shared-ptr

//简单模拟实现boost库下的shared_ptr #include <iostream> #include <string.h> using namespace std; class shared_ptr_Rep { friend class shared_ptr; public: shared_ptr_Rep(const char *str = " ") :count(0) { px = new char[strlen(str) + 1]; strcpy(