扩展progress_timer的计时精度
progress对外输出精度只有小数点后两位(这点可以运行上节程序进行验证),即精确到0.01秒。
我们使用模板技术仿造一个progress_timer编写一个新类:new_progress_timer,以实现任意精度的输出。
new_progress_timer同样继承自timer,只是编程了模板类。模板参数N指明了输出精度,默认值为2,与progress_timer相同。
#include <boost\timer.hpp>
#include <boost\progress.hpp>
#include <boost\static_assert.hpp>
#include <iostream>
using namespace boost;
using namespace std; //使用模板参数实现progress_timer
template<int N=>
class new_progress_timer:public boost::timer
{
public:
new_progress_timer(std::ostream &os=std::cout):m_os(os)
{
BOOST_STATIC_ASSERT(N>=&&N<=);
} ~new_progress_timer(void)
{
try{
//保持流的状态
std::istream::fmtflags old_flags=m_os.setf(std::istream::fixed,std::istream::floatfield); std::streamsize old_prec=m_os.precision(N); m_os<<elapsed()<<"s\n"<<std::endl; m_os.flags(old_flags);
m_os.precision(old_prec); }
catch( ...)
{
}
} private:
std::ostream &m_os;
}; //当精度为2时,使用下面这个 template<>
class new_progress_timer<>:public boost::progress_timer
{}; int main()
{
new_progress_timer<> t; //声明一个计时器,开始计时
//dosomething
for(int i=;i<;i++)
{
cout<<"a";
}
cout<<endl;
}
扩展progress_timer的计时精度的更多相关文章
- 前端Tips#4 - 用 process.hrtime 获取纳秒级的计时精度
		
本文同步自 JSCON简时空 - 前端Tips 专栏#4,点击阅读 视频讲解 视频地址 文字讲解 如果去测试代码运行的时长,你会选择哪个时间函数? 一般第一时间想到的函数是 Date.now 或 Da ...
 - Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
		
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
 - Linux用户态程序计时方式详解
		
前言 良好的计时器可帮助程序开发人员确定程序的性能瓶颈,或对不同算法进行性能比较.但要精确测量程序的运行时间并不容易,因为进程切换.中断.共享的多用户.网络流量.高速缓存访问及转移预测等因素都会对程序 ...
 - Linux用户态程序计时方式详解[转]
		
转自: http://www.cnblogs.com/clover-toeic/p/3845210.html 前言 良好的计时器可帮助程序开发人员确定程序的性能瓶颈,或对不同算法进行性能比较.但要精确 ...
 - VC中如何获取当前时间(精度达到毫秒级)
		
标 题: VC中如何获取当前时间(精度达到毫秒级)作 者: 0xFFFFCCCC时 间: 2013-06-24链 接: http://www.cnblogs.com/Y4ng/p/Millisecon ...
 - 【转】VxWorks中高精度实时时钟的实现及C语言汇编混合编程
		
最近一个项目中需要在VxWorks下使用一个高精度实时时钟,要求精度为1ms,溢 出时间大于5小时.VxWorks提供系统时钟,该时钟在操作系统启动后开始计数,精度为1个tick,可以通过tickGe ...
 - 【C/C++】计时函数比较
		
目前,存在着各种计时函数,一般的处理都是先调用计时函数,记下当前时间tstart,然后处理一段程序,再调用计时函数,记下处理后的时间tend,再tend和tstart做差,就可以得到程序的执行时间,但 ...
 - clock()函数的返回值精度问题
		
clock()函数返回值为1毫秒,就是0.001秒.clock函数功 能: 返回处理器调用某个进程或函数所花费的时间.用 法: clock_t clock(void);说明:clock_t其实就是lo ...
 - CUDA 8的混合精度编程
		
CUDA 8的混合精度编程 Volta和Turing GPU包含 Tensor Cores,可加速某些类型的FP16矩阵数学运算.这样可以在流行的AI框架内更快,更轻松地进行混合精度计算.要使用Ten ...
 
随机推荐
- Nginx+uwsgi安装配置
			
一.安装基础开发包 yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-dev ...
 - delphi 导出xml文件
			
procedure TfrmTallageDetail.ToolButton1Click(Sender: TObject); var Xml: TXMLDocument; Rootbusiness,R ...
 - Leetcode: Validate IP Address
			
In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...
 - Leetcode: Sort Transformed Array
			
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
 - 我的第一个开源项目-logger4Net
			
学会学习,学会分享,学会创造. 这我的第一个开源项目.以前总觉得开源离自己很远,但通过不断学习发现,其实自己已经走在这条路上.思想就在那,只要懂得学习,懂得分享,就会懂享受创造的快乐感与殷实感. 没做 ...
 - Android系统调用
			
android 中intent是经常要用到的.不管是页面牵转,还是传递数据,或是调用外部程序,系统功能都要用到intent. 在做了一些intent的例子之后,整理了一下intent,希望对大家有用. ...
 - CentOs下安装maven
			
现有的一个项目使用了Maven来管理,源代码放到了Subversion中.虽然Maven管理项目很方便,但是部署起来还是很麻烦的.先要在本地生成项目jar包,上传到服务器,然后再重启服务.如果在服务器 ...
 - eBox(stm32) 之中断结构
			
eBox的中断结构参考了mbed,和我们平时所用的中断结构有些差异,不容易理解,最近仔细看了底层代码,终于搞清楚了,总结一下 一 首先要要搞清楚的几个概念:类的静态成员,实例成员 ...
 - html5,视频的兼容
			
<video controls="controls" width="500" > <source src="1.mp4 ...
 - 为什么C#中要设计IntPtr?
			
示例代码: IntPtr vertex = someObj.Get().Lock(0, someObj.Get().GetSizeInBytes(), HardwareBuffer.LOCKOPTIO ...