使用Boost中的Timer库计算程序的运行时间

程序开发人员都会面临一个共同的问题,即写出高质量的代码完毕特定的功能。评价代码质量的一个重要标准就是算法的运行效率,也就是算法的运行时间。为了可靠的提高程序的运行效率,首先要知道运行程序所消耗的时间,然后找出可行的方案对程序进行优化。C++程序猿在开发代码的过程中难免会遇见此类问题,本文以Boost中的Timer库为例。具体解说怎样測量程序的运行时间。

Boost中Timer库的介绍

Timer是Boost中的一个非常小的时间库。提供时间度量和进度显示功能。当中包括三个组件:(1)计时器类timer、timer类的子类progress_timer类和进度指示类progress_display。

1、 timer

timer位于boost命名空间中,使用之前须要包括头文件<boost/timer.hpp>

timer中有3个函数,分别为:(1)elapsed_max(),返回可度量的最大时间间隔;(2)elapsed_min(),返回可度量的最小时间间隔。(3)elapsed(),返回timer类创建到elapsed()函数调用时所流逝的时间。

比如採用timer类測量std::cout<<"helloworld"<<std::endl;语句的运行时间,程序例如以下所看到的:

#include<iostream>

#include<cstdlib>

using namespace std;

#include <boost/timer.hpp>

using namespace boost;

int main(int argc, char ** argv)

{

timer ter; //创建对象时就開始计时

std::cout<<"helloworld"<<std::endl;

std::cout<<ter.elapsed()<<std::endl;//输出程序运行所消耗的时间,以秒为单位。

std::cout<<ter.elapsed_max()<<std::endl;//输出timer类可以度量的最大时间间隔。以秒为单位。

std::cout<<ter.elapsed_min()<<std::endl;//输出timer类可以度量的最小时间间隔。以秒为单位。

return EXIT_SUCCESS;

}

程序运行结果例如以下图所看到的:

2 、 progress_timer类

progress_timer类是timer类的子类,此类具有一定的特殊性,在系统对此类对象进行析构时会自己主动调用此类的elapsed()函数。输出系统运行程序所消耗的时间。

使用progress_timer类须要包括<boost/progress.hpp>头文件

#include <iostream>

#include <cstdlib>

using namespace std;





#include <boost/progress.hpp>

using namespace boost;





int main(int argc, char **argv)

{

boost::progress_timer pt;

std::cout<<"helloworld"<<std::endl;

std::cout<<pt.elapsed()<<std::endl;//手动调用elapsed()函数,输出程序运行时间



return EXIT_SUCCESS;

}

结果例如以下图所看到的:

3、progress_display类

progress_display类用于显示程序的运行进度。使得用户获得动态感。

使用前须要包括<boost/progress.hpp>头文件

#include <iostream>

#include <cstdlib>

#include <vector>

using namespace std;





#include <boost/progress.hpp>

using namespace boost;





int main(int argc, char **argv)

{

std::vector<int> vec(10000);



//申明进度条

boost::progress_display pd(vec.size());



for (int i=0; i<100; i++)

{

vec.push_back(i);

}





return EXIT_SUCCESS;

}

程序运行结果例如以下图所看到的:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY213MjAwOGZyZWU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

Boost中的Timer的使用——计算时间流逝的更多相关文章

  1. boost.asio系列——Timer

    同步Timer asio中提供的timer名为deadline_timer,它提供了超时计时的功能.首先以一个最简单的同步Timer为例来演示如何使用它. #include<iostream&g ...

  2. boost之时间timer

    C++一直缺乏对时间和日期的处理能力,一般借助于C的struct tm和time():timer包含三个类其中timer,progress_timer是计时器类,进度指示类是progress_disp ...

  3. iOS中的NSTimer 和 Android 中的Timer

    首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...

  4. .NET中的Timer类型用法详解

    这篇文章主要介绍了.NET中的Timer类型用法,较为详细的分析了Timer类型在各种环境下的用法,需要的朋友可以参考下   在.NET FrameWork中有多个Timer,那么怎么根据实际情况进行 ...

  5. 【JavaScript】JavaScript中的Timer是怎么工作的( setTimeout,setInterval)

    原文(http://www.yeeyan.org/articles/view/luosheng/24380) 作为入门者来说,了解JavaScript中timer的工作方式是很重要的.通常它们的表现行 ...

  6. Android中图片占用内存的计算

    Android中图片占用内存的计算 原文链接 http://blog.sina.com.cn/s/blog_4e60b09d01016133.html   在Android开发中,我现在发现很多人还不 ...

  7. Boost中的智能指针(转)

    这篇文章主要介绍 boost中的智能指针的使用.(转自:http://www.cnblogs.com/sld666666/archive/2010/12/16/1908265.html) 内存管理是一 ...

  8. ASP.NET AJAX入门系列(11):在多个UpdatePanle中使用Timer控件

    本文将使用Timer控件更新两个UpdatePanel控件,Timer控件将放在UpdatePanel控件的外面,并将它配置为UpdatePanel的触发器,翻译自官方文档. 主要内容 在多个Upda ...

  9. C# windows服务:C#windows服务中的Timer控件的使用

    C# windows服务程序中的Timer控件的使用问题是如何解决的呢? 今天和同事一起研究了下C# windows服务程序中的Timer控件的使用的写法. 我们在建立一个C# windows服务程序 ...

随机推荐

  1. MySQL sys Schema

    MySQL sys Schema 使用sys Schema的先决条件 使用sys Schema sys Schema Progress Reporting sys Schema Object Refe ...

  2. GO:interface

    一.感受接口 type Usb interface { Connect() Disconnect() } // 手机 type Phone struct {} // 相机 type Camera st ...

  3. 微信小程序request请求动态获取数据

    微信小程序开发文档链接 1 后台代码: clickButton:function(){ var that = this; wx.request({ url: 'http://localhost:909 ...

  4. 关于最新版本react-native0.59.x构建的问题解决方案

    react-native的版本更新是真的快,几乎几天就是一个小版本,然而在这个过程中,对于新手来说,成功构建一个,并跑起来的项目,还是有一定难度的,各种问题,一不小心,你就会发现你的时间全部都浪费在了 ...

  5. pwnable flag之write up

    Papa brought me a packed present! let's open it. Download : http://pwnable.kr/bin/flag This is rever ...

  6. LeetCode(100) Same Tree

    题目 Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  7. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  8. 【HDU 1402】A * B Problem Plus(FFT)

    Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to e ...

  9. hihoCoder#1196 : 高斯消元·二(开关灯问题)

    传送门 高斯消元解异或方程组 小Ho在游戏板上忙碌了30分钟,任然没有办法完成,于是他只好求助于小Hi. 小Ho:小Hi,这次又该怎么办呢? 小Hi:让我们来分析一下吧. 首先对于每一个格子的状态,可 ...

  10. POJ1159:Palindrome【dp】

    题目大意:给出一个字符串,问至少添加多少个字符才能使它成为回文串? 思路:很明显的方程是:dp[i][j]=min{dp[i+1][j],dp[i][j-1],dp[i+1][j-1](str[i]= ...