Boost学习笔记——cpu_timer

原始版本的timer已经废弃,最新的版本建议使用cpu_timer。

一、简介:

这个类一般用来记录程序运行了多长时间。它被分解为经过时间,操作系统响应用户请求的时间和用户时间。

cpu_timer计量经过时间,用户执行时间,和系统执行时间。auto_cpu_timer是一个更为精细的cpu_timer,当它被销毁时能自动的报告花费的时间。

 

二、使用cpu_timer

例一:

 1 #include <boost/timer/timer.hpp>
 2 #include <cmath>
 3 
 4 int main()
 5 {
 6   boost::timer::auto_cpu_timer t;
 7 
 8   for (long i = 0; i < 100000000; ++i)
 9     std::sqrt(123.456L); // burn some time
10 
11   return 0;
12 }

当auto_cpu_timer构造的时候,计时开始,当它析构的时候,则自动的在默认的输出流std::cout中输出花费的时间,并停止计时。

输出格式如下:

 5.713010s wall, 5.709637s user + 0.000000s system = 5.709637s CPU (99.9%)

可以在构造cpu_timer的时候改变输出格式。下图为一些常用的输出形式:

 

例二:

 1 #include <boost/timer/timer.hpp>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <sstream>
 5 #include <string>
 6 
 7 int _tmain(int argc, _TCHAR* argv[])
 8 {
 9     std::stringstream ss;
10     std::string str;
11     {
12         boost::timer::auto_cpu_timer t(ss,2);
13         for (long i = 0; i < 100000000; ++i)
14             std::sqrt(123.456L); // burn some time
15 
16     }
17     ss >> str;
18     std::cout << str << std::endl;
19     
20     return 0;
21 }

 

 

 

 

转载于:https://www.cnblogs.com/nangning/p/3327901.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值