boost timer
Boost.Timer provides clocks to measure code performance. At first, it may seem like this library competes with Boost.Chrono. However, while Boost.Chrono provides clocks to measure arbitrary periods, Boost.Timer measures the time it takes to execute code. Although Boost.Timer uses Boost.Chrono, when you want to measure code performance, you should use Boost.Timer rather than Boost.Chrono.
1. cpu_timer
#include <boost/timer/timer.hpp>
#include <iostream>
#include <cmath> using namespace boost::timer; int main()
{
cpu_timer timer; for (int i = ; i < ; ++i)
std::pow(1.234, i);
std::cout << timer.format() << std::endl;
return ;
}
Measurement starts when boost::timer::cpu_timer is instantiated. You can call the member function format() at any point to get the elapsed time.
输出为:
0.057313s wall, 0.050000 user + 0.000000s system = 0.050000 CPU (87.2%)
The wall time is the time which passes according to a wall clock. The CPU time says how much time the program spent executing code. CPU time is divided between time spent in user space and time spent in kernel space.
2. stopping and resuming timers
#include <boost/timer/timer.hpp>
#include <iostream>
#include <cmath> using namespace boost::timer; int main()
{
cpu_timer timer; for (int i = ; i < ; ++i)
std::pow(1.234, i);
std::cout << timer.format() << std::endl; timer.stop(); for (int i = ; i < ; ++i)
std::pow(1.234, i);
std::cout << timer.format() << std::endl; timer.resume(); for (int i = ; i < ; ++i)
std::pow(1.234, i);
std::cout << timer.format() << std::endl;
return ;
}
boost::timer::cpu_timer provides the member functions stop() and resume(), which stop and resume timers. boost::timer::cpu_timer also provides a member function start(). If you call start(), the timer restarts from zero.
3. getting wall and cpu time as a tuple
#include <boost/timer/timer.hpp>
#include <iostream>
#include <cmath> using namespace boost::timer; int main()
{
cpu_timer timer; for (int i = ; i < ; ++i)
std::pow(1.234, i); cpu_times times = timer.elapsed();
std::cout << times.wall << std::endl;
std::cout << times.user << std::endl;
std::cout << times.system << std::endl;
return ;
}
boost::timer::cpu_timer provides the member function elapsed(). elapsed() returns a tuple of type boost::timer::times. This tuple has three member varibles: wall, user and system. These member variables contain the wall and CPU times in nanoseconds. boost::timer::times provides the member function clear() to set wall, user and system to 0.
4. auto_cpu_timer
#include <boost/timer/timer.hpp>
#include <cmath> using namespace boost::timer; int main()
{
auto_cpu_timer timer; for (int i = ; i < ; ++i)
std::pow(1.234, i);
return ;
}
The destructor of this class stops measuring time and writes the time to the standard output stream.
boost timer的更多相关文章
- boost::timer库使用
boost::timer boost库定时器使用,需要在编译时加相关链接库 -lboost_timer -lboost_system boost::timer::cpu_timer 和boost::t ...
- boost::timer demo
#include <iostream> #include <boost/timer.hpp> //timer的头文件 using namespace boost; //打开bo ...
- boost timer 定时器 Operation cancel !
前面段时间处理一个定时器时,老是提示 操作取消. 硬是没搞明白为什么! 其实我遇到的这个情况很简单就是(boost::asio::deadline_timer timer)这个变量的生命同期结束了,对 ...
- boost timer代码学习笔记
socket连接中需要判断超时 所以这几天看了看boost中计时器的文档和示例 一共有五个例子 从简单的同步等待到异步调用超时处理 先看第一个例子 // timer1.cpp: 定义控制台应用程序的入 ...
- 初探boost之timer库学习笔记
timer 使用方法 #include <boost/timer.hpp> #include <iostream> using namespace std; usi ...
- boost 学习笔记 2: timer
boost 学习笔记 2: timer copy from:http://einverne.github.io/post/2015/12/boost-learning-note-2.html 1:ti ...
- boost之时间timer
C++一直缺乏对时间和日期的处理能力,一般借助于C的struct tm和time():timer包含三个类其中timer,progress_timer是计时器类,进度指示类是progress_disp ...
- Boost中的Timer的使用——计算时间流逝
使用Boost中的Timer库计算程序的运行时间 程序开发人员都会面临一个共同的问题,即写出高质量的代码完毕特定的功能.评价代码质量的一个重要标准就是算法的运行效率,也就是算法的运行时间.为了可靠的提 ...
- Ubuntu 14.04 编译安装 boost 1.58
简介 Boost is a set of libraries for the C++ programming language that provide support for tasks and s ...
随机推荐
- Java控制台输入字符串及字符串比较
需求描述:茵茵很喜欢研究车牌号码,从车牌号码上可以看出号码注册的早晚,据研究发现,车牌号码是按字典序发放的,现在她收集了很多车牌号码,请你设计程序帮她判断注册较早的号码.车牌号码由5个字母或数字组成. ...
- jenkins展示html测试报告(不使用html publisher)
最终效果: 点击『测试报告』,查看测试报告: 一开始的思路是: jenkins上展示html报告[转载] jenkins使用HTML Publisher Plugin插件 拉取报告样式缺失问题解决 ...
- Vagrant 手册之同步目录 - VirtualBox
原文地址 如果你使用的 provider 是 VirtualBox,那么 VirtualBox 同步目录就是默认的同步目录类型.这些同步目录使用 VirtualBox 的共享目录系统来同步客户机跟宿主 ...
- SVG绘制多个圆
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (转载)Java 8 认识 HashMap
原链接:传送门 摘要 HashMap是Java程序员使用频率最高的用于映射(键值对)处理的数据类型.随着JDK(Java Developmet Kit)版本的更新,JDK1.8对HashMap底层的实 ...
- MySQL数据库忘记密码如何重新设置?
前 言当我们忘记了MySQL数据库密码后,该如何重新进行设置? 操作步骤步骤1:cmd打开命名窗口 步骤2:关闭正在运行的MySQL服务(命令:net stop mysql)(如果:此时MySQL正在 ...
- jmeter:清除本地指定目录下的所有类型文件
1,创建一个sampler 2,要在本地有一个目录的文件 3,直接上代码 String path = "C:\\临时文件\\test111" ; File file ...
- sed删除注释行和空行
典型需求: 删除nginx.conf文件中注释行和空行 sed -i '/^#/d;/^$/d' nginx.conf 删除一个或多个空格加 # 号的行 sed -i '/[:blank:]*#/d' ...
- 简单谈谈Netty的高性能之道
传统RPC 调用性能差的三宗罪 网络传输方式问题:传统的RPC 框架或者基于RMI 等方式的远程服务(过程)调用采用了同步阻塞IO,当客户端的并发压力或者网络时延增大之后,同步阻塞IO 会由于频繁的w ...
- 难倒你了吧!ArrayList 为啥要实现 RandomAccess 接口?
作者:蔡先森_caiyq https://www.jianshu.com/p/3e2a9e4c9e01 在我们的开发中,List接口是最常见不过,而且我们几乎每天都在用ArrayList或者Linke ...