使用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="">

最新文章

  1. winform采集网站美女图片程序---多线程篇
  2. 浏览器兼容性之JavaScript篇
  3. vb.net下载代码
  4. XUnit的使用
  5. CentOS7安装RabbitMQ集群
  6. scrapy 知乎的模拟登陆及抓取用户数据
  7. c#+Unity3D
  8. 【C++】undered_map的用法总结(1)
  9. Java Date类的使用总结
  10. UserControl 的一个值得注意的问题 [属性&quot; * &quot;的代码生成失败.错误是:&quot;程序集&quot;*.Version=1.0.0.0,Culture=neutral,..........无标记为序列化&quot;
  11. MOBA项目定点数的一个想法
  12. Linux inode空间占满 “no space left on device”
  13. SpringMVC multipart文件上传
  14. Oracle从字符串资源中得到想要的数据分析
  15. 【BZOJ4108】[Wf2015]Catering 有上下界费用流
  16. TinkerPop中的遍历:图的遍历策略
  17. MVC中不能使用ViewBag
  18. [python学习篇 ] subprocess 子进程
  19. css 标题
  20. mediaplayer state

热门文章

  1. Linux组和提权
  2. C语言程序内存分布
  3. 数据结构( Pyhon 语言描述 ) — — 第2章:集合概览
  4. C语言学习10
  5. 对于easyui $.messager.alert和 $.messager.confirm 不同浏览器中位置控制
  6. SpringCloud源码地址
  7. 使用SqlParameter.SqlDbType和SqlParameter.Size时需要注意的地方
  8. [luoguP2704] 炮兵阵地(状压DP)
  9. [luoguP3606] [USACO17JAN]Building a Tall Barn建谷仓(贪心 + 线段树)
  10. 洛谷P1145 约瑟夫